WPF datagrid行双击事件 [英] WPF datagrid row double click event

查看:168
本文介绍了WPF datagrid行双击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要在双击datagrid行时获得选定的行值。我发现我们需要双击这一点。无论我们点击网格,鼠标双击事件都会触发。要仅在选择datagrid行时获取值,我已编写以下代码



XAML:

 <   datagrid     autogeneratecolumns   =  False   名称  =  dgTitlesPendingListofTitles    canuseraddrows   =  False    itemssource   =  {Binding}    mousedoubleclick   =  dgTitlesPendingListofTitles_MouseDoubleClick >  
< datagrid.columns >
< datagridtextcolumn 标题 = 艺术家 - 标题 binding = {绑定路径= ArtistTitle} 可见性 = 可见 w宽度 = * isreadonly = False / >
< / datagrid.columns >
< / datagrid >





代码落后:



 < span class =code-keyword> private   void  dgTitlesPendingListofTitles_MouseDoubleClick( object  sender,MouseButtonEventArgs e )
{
IInputElement ele ment = e.MouseDevice.DirectlyOver;
if (element!= null && element FrameworkElement)
{
if (((FrameworkElement)element).Parent DataGridCell)
{
var grid = sender as DataGrid;
if (grid!= null && grid.SelectedItems!= null && grid.SelectedItems.Count == 1
{
var rowview = grid.SelectedItem as DataRowView;
if (rowview!= null
{
DataRow行= rowview.Row;
}
}
}
}
}





我是获取grid.SelectedItems.Count = 0所以它不会进入那个循环。任何人都可以帮我解决问题,或者我在这里犯的是什么错误。

解决方案

< blockquote>从DirectlyOver获取所选元素将返回一个IInputElement,其中Parent始终为null,因此永远不会输入第二个 if 循环。如果您只想从DoubleClickEvent获取所选行,则可以执行以下操作:



  private   void  dgTitlesPendingListofTitles_MouseDoubleClick( object  sender,MouseButtonEventArgs e)
{
if (sender!= null
{
DataGrid grid = sender as DataGrid;
if (grid!= null && grid.SelectedItems!= null && grid.SelectedItems.Count == 1
{
DataGridRow dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
}
}
}





希望这有帮助


< blockquote>将RowStyle添加到DataGrid

 <   DataGrid     AutoGenerateColumns   =  False    Grid.Row   =  2   保证金  =  8,22,12,36   名称  =  resultDataGrid     IsReadOnly   =  True   >  
< DataGrid.RowStyle >
< 样式 TargetType = {x:输入DataGridRow} >
< EventSetter 事件 = MouseDoubleClick 处理程序 = resultDataGrid_MouseDoubleClick / >
< / Style >
< / DataGrid.RowStyle >
< / DataGrid >



代码背后:

 private void resultDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
if(sender!= null)
{
DataGridRow dgr = sender as DataGridRow;
}
}


Hi,

I am need to get a selected row value on double click of datagrid row. I found we need mousedouble click for this. Mouse double click event fires wherever we click on grid. To get value only on selection of the datagrid row I have written the following code

XAML:

<datagrid autogeneratecolumns="False" name="dgTitlesPendingListofTitles" canuseraddrows="False" itemssource="{Binding}" mousedoubleclick="dgTitlesPendingListofTitles_MouseDoubleClick">
                        <datagrid.columns>
                            <datagridtextcolumn header="Artists-Titles" binding="{Binding Path=ArtistTitle}" visibility="Visible" width="*" isreadonly="False" />
                            </datagrid.columns>
                    </datagrid>



Code behind:

private void dgTitlesPendingListofTitles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            IInputElement element = e.MouseDevice.DirectlyOver;
            if (element != null && element is FrameworkElement)
            {
                if (((FrameworkElement)element).Parent is DataGridCell)
                {
                    var grid = sender as DataGrid;
                    if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                    {
                        var rowview = grid.SelectedItem as DataRowView;
                        if (rowview != null)
                        {
                            DataRow row = rowview.Row;
                        }
                    }
                }
            }
}



I am getting grid.SelectedItems.Count =0 so it is not going into that loop.Can anyone help me out with a solution or what is the mistake I am doing here.

解决方案

Getting the selected element from DirectlyOver will return an IInputElement where the Parent is always null, therefore your second if loop will never get entered. If you only want to get the selected row from the DoubleClickEvent then you can do this:

private void dgTitlesPendingListofTitles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
       {
           if (sender != null)
           {
               DataGrid grid = sender as DataGrid;
               if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
               {
                   DataGridRow dgr = grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem) as DataGridRow;
               }
           }
       }



Hope this helps


Add RowStyle to DataGrid

<DataGrid AutoGenerateColumns="False" Grid.Row="2" Margin="8,22,12,36" Name="resultDataGrid" IsReadOnly="True" >
            <DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <EventSetter Event="MouseDoubleClick" Handler="resultDataGrid_MouseDoubleClick"/>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>


And in code behind:

private void resultDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
       {
           if (sender != null)
           {
                   DataGridRow dgr = sender as DataGridRow;
           }
       }


这篇关于WPF datagrid行双击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆