【问题标题】:C# WPF Handling double click events from multiple, dynamically generate datagrids through a single handlerC# WPF 处理来自多个的双击事件,通过单个处理程序动态生成数据网格
【发布时间】:2011-10-13 22:10:22
【问题描述】:

在我当前的实现中,我动态生成选项卡和网格。

基本上,需要通过双击前一个网格的任意一行来创建一个新的网格,并将该行数据用于其他证明。

this.AddHandler(DataGrid.MouseDoubleClickEvent, new RoutedEventHandler    (Generic_DoubleClick));    

即使在网格之外,也可以处理任何双击,而不是专门针对网格。

我需要找到一个可以将行值专门返回到该网格的处理程序。请提出解决方法或更简单的方法。

谢谢。

【问题讨论】:

    标签: c# wpf wpf-controls wpfdatagrid


    【解决方案1】:

    从数据网格的数据网格行处理双击路由事件。

       <tk:DataGrid>
            <tk:DataGrid.Resources>
                <Style TargetType="{x:Type tk:DataGridRow}">
                    <EventSetter Event="MouseDoubleClick"
                                 Handler="DataGridRow_MouseDoubleClick"/>
                </Style>
            </tk:DataGrid.Resources>
            <tk:DataGrid.ItemsSource>
                <x:Array Type="{x:Type TextBlock}">
                    <TextBlock Text="1" Tag="1.1"/>
                    <TextBlock Text="2" Tag="1.2"/>
                    <TextBlock Text="3" Tag="1.3"/>
                    <TextBlock Text="4" Tag="1.4"/>
                </x:Array>
            </tk:DataGrid.ItemsSource>
            <tk:DataGrid.Columns>
                <tk:DataGridTextColumn Header="Text" Binding="{Binding Text}"/>
                <tk:DataGridTextColumn Header="Tag" Binding="{Binding Tag}"/>
            </tk:DataGrid.Columns>
        </tk:DataGrid>
    

    在后面的代码中

        private void DataGridRow_MouseDoubleClick(
               object sender, MouseButtonEventArgs e)
        {
            var dgRow = sender as Microsoft.Windows.Controls.DataGridRow;
            var cellContentElement = e.OriginalSource as UIElement;
        }
    

    Bonus 是 cellContentElement 是在该行上双击的单元格的内容元素...例如如果是DataGridTextColumn,则单元格中将是TextBlock。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多