如果按以下绑定,DataGrid 模板里的Radio 的绑定事件将不会被触发:
< sdk:DataGrid Grid.Column = "0" x:Name = "dataGrid" IsReadOnly = "True" AutoGenerateColumns = "False" Width = "auto" Height = "auto" HorizontalAlignment = "Left" VerticalAlignment = "Top" ItemsSource = "{Binding NoticeCollection,Mode=TwoWay}" IsTabStop = "False" Style = "{StaticResource DataGridStyle}" ColumnHeaderStyle = "{StaticResource DataGridColumnHeaderStyle}" RowStyle = "{StaticResource DataGridRowStyle}" > < i:Interaction.Triggers > < i:EventTrigger EventName = "MouseLeftButtonDown" > < ei:CallMethodAction MethodName = "DatagridMouseLeftButtonDown" TargetObject = "{Binding}" > </ ei:CallMethodAction > </ i:EventTrigger > </ i:Interaction.Triggers > < sdk:DataGrid.Columns > < sdk:DataGridTemplateColumn Header = "选项" Width = "0.1*" > < sdk:DataGridTemplateColumn.CellTemplate > < DataTemplate > < StackPanel HorizontalAlignment = "Center" VerticalAlignment = "Top" Margin = "0,7,0,0" > < RadioButton GroupName = "sel" Command = "{Binding MessageSelected}" CommandParameter = "{Binding Id,Mode=OneWay}" ></ RadioButton > </ StackPanel > </ DataTemplate > </ sdk:DataGridTemplateColumn.CellTemplate > </ sdk:DataGridTemplateColumn > < sdk:DataGridTemplateColumn Header = "标题" Width = "0.3*" > < sdk:DataGridTemplateColumn.CellTemplate > < DataTemplate > < StackPanel HorizontalAlignment = "Left" VerticalAlignment = "Top" Margin = "0,7,0,0" > < HyperlinkButton Content = "{Binding Title,Mode=OneWay}" Style = "{StaticResource PageHyperlinkButtonStyle}" Command = "{Binding Source={StaticResource Locator},Path=NoticeListViewModelStatic.ViewMsgCommand}" CommandParameter = "{Binding Id,Mode=OneWay}" /> </ StackPanel > </ DataTemplate > </ sdk:DataGridTemplateColumn.CellTemplate > </ sdk:DataGridTemplateColumn > < sdk:DataGridTextColumn Header = "信息内容" Width = "0.4*" Binding = "{Binding Content,Mode=OneWay}" ></ sdk:DataGridTextColumn > <!--<sdk:DataGridTextColumn Header="创建时间" Width="0.2*" Binding="{Binding CreateTime,Mode=OneWay,StringFormat='yyy-MM-dd HH:mm'}"></sdk:DataGridTextColumn>--> < sdk:DataGridTemplateColumn Header = "创建时间" Width = "0.2*" > < sdk:DataGridTemplateColumn.CellTemplate > < DataTemplate > < StackPanel HorizontalAlignment = "Center" VerticalAlignment = "Top" Margin = "0,7,0,0" > < TextBlock Text = "{Binding CreateTime,Mode=OneWay,StringFormat='yyy-MM-dd HH:mm'}" /> </ StackPanel > </ DataTemplate > </ sdk:DataGridTemplateColumn.CellTemplate > </ sdk:DataGridTemplateColumn > </ sdk:DataGrid.Columns > </ sdk:DataGrid > |
原因是DataGrid.Columns 不是依赖属性,所有绑定的dataContext将会丢失,所有可以用element-to-element的数据绑定来解决此问题
< sdk:DataGrid Grid.Column = "0" x:Name = "dataGrid" IsReadOnly = "True" AutoGenerateColumns = "False" Width = "auto" Height = "auto" HorizontalAlignment = "Left" VerticalAlignment = "Top" ItemsSource = "{Binding NoticeCollection,Mode=TwoWay}" IsTabStop = "False" Style = "{StaticResource DataGridStyle}" ColumnHeaderStyle = "{StaticResource DataGridColumnHeaderStyle}" RowStyle = "{StaticResource DataGridRowStyle}" > < i:Interaction.Triggers > < i:EventTrigger EventName = "MouseLeftButtonDown" > < ei:CallMethodAction MethodName = "DatagridMouseLeftButtonDown" TargetObject = "{Binding}" > </ ei:CallMethodAction > </ i:EventTrigger > </ i:Interaction.Triggers > < sdk:DataGrid.Columns > < sdk:DataGridTemplateColumn Header = "选项" Width = "0.1*" > < sdk:DataGridTemplateColumn.CellTemplate > < DataTemplate > < StackPanel HorizontalAlignment = "Center" VerticalAlignment = "Top" Margin = "0,7,0,0" > < RadioButton GroupName = "sel" Command = "{Binding Source={StaticResource Locator},Path=NoticeListViewModelStatic.MessageSelected}" CommandParameter = "{Binding Id,Mode=OneWay}" ></ RadioButton > </ StackPanel > </ DataTemplate > </ sdk:DataGridTemplateColumn.CellTemplate > </ sdk:DataGridTemplateColumn > < sdk:DataGridTemplateColumn Header = "标题" Width = "0.3*" > < sdk:DataGridTemplateColumn.CellTemplate > < DataTemplate > < StackPanel HorizontalAlignment = "Left" VerticalAlignment = "Top" Margin = "0,7,0,0" > < HyperlinkButton Content = "{Binding Title,Mode=OneWay}" Style = "{StaticResource PageHyperlinkButtonStyle}" Command = "{Binding Source={StaticResource Locator},Path=NoticeListViewModelStatic.ViewMsgCommand}" CommandParameter = "{Binding Id,Mode=OneWay}" /> </ StackPanel > </ DataTemplate > </ sdk:DataGridTemplateColumn.CellTemplate > </ sdk:DataGridTemplateColumn > < sdk:DataGridTextColumn Header = "信息内容" Width = "0.4*" Binding = "{Binding Content,Mode=OneWay}" ></ sdk:DataGridTextColumn > <!--<sdk:DataGridTextColumn Header="创建时间" Width="0.2*" Binding="{Binding CreateTime,Mode=OneWay,StringFormat='yyy-MM-dd HH:mm'}"></sdk:DataGridTextColumn>--> < sdk:DataGridTemplateColumn Header = "创建时间" Width = "0.2*" > < sdk:DataGridTemplateColumn.CellTemplate > < DataTemplate > < StackPanel HorizontalAlignment = "Center" VerticalAlignment = "Top" Margin = "0,7,0,0" > < TextBlock Text = "{Binding CreateTime,Mode=OneWay,StringFormat='yyy-MM-dd HH:mm'}" /> </ StackPanel > </ DataTemplate > </ sdk:DataGridTemplateColumn.CellTemplate > </ sdk:DataGridTemplateColumn > </ sdk:DataGrid.Columns > </ sdk:DataGrid > |
顺便提一下,silverlight的dataGrid在MVVM模式下的排序事件实现:
在View里声明出发前:
< i:Interaction.Triggers > < i:EventTrigger EventName = "MouseLeftButtonDown" > < ei:CallMethodAction MethodName = "DatagridMouseLeftButtonDown" TargetObject = "{Binding}" > </ ei:CallMethodAction > </ i:EventTrigger > </ i:Interaction.Triggers > |
在ViewModel里实现方法:
public void DatagridMouseLeftButtonDown( object sender, MouseButtonEventArgs e) { var u = from element in VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition( null ), sender as UIElement) where element is DataGridColumnHeader select element; if (u.Count() == 1) { e.Handled = true ; var columnTag = ConvertColumnName(((DataGridColumnHeader)u.Single()).Content.ToString()); if ( string .IsNullOrEmpty(columnTag) || columnTag == "Serial" || columnTag == "Do" ) return ; Ascning = (OrderColumn == columnTag) && !Ascning; OrderColumn = columnTag; PagerInfo p = new PagerInfo(); p.PageSize = this ._pagesize; p.PageIndex = 1; this .PagerContext.MoveToFirstPage(); SortType = Ascning ? "ASC" : "DESC" ; searchData(p, OrderColumn, SortType); } } |