zoukankan      html  css  js  c++  java
  • MVVMLight模式,在dataGrid的模板下,绑定事件不触发的原因已经服务端排序的实现

    如果按以下绑定,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); 
            
         }
  • 相关阅读:
    【Vegas原创】读写cookies
    【Vegas原创】ComponentArt经典使用(饼图)
    【Vegas原创】SQL Server调用CDO发送邮件
    【Vegas原创】页面(图表+table+GridView)导出为excel(07125更新版)
    【Vegas原创】Procedure经典用法~(需配合DBAccess类)
    【Vegas原创】CDO发送邮件
    【Vegas原创】System.Net.Mail(.net2.0)或System.Web.Mail(.NET1.x) 发送邮件
    HTTP 500 内部服务器错误问题08.5.8Update
    【Vegas原创】Jmail发送邮件(Vegas Final版)2007年10月22日UPDATE:正文中图片的显示
    【Vegas原创】a href="#" onclick=""的一个小技巧
  • 原文地址:https://www.cnblogs.com/allexw/p/2442861.html
Copyright © 2011-2022 走看看