具有SelectionMode =“多个”的WPF DataGrid作为ListView? [英] WPF DataGrid with SelectionMode="Multiple" as ListView?

查看:145
本文介绍了具有SelectionMode =“多个”的WPF DataGrid作为ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于WPF DataGrid,您有2种选择模式:单一和扩展。

对于WPF ListView,您有3种选择模式:单一,多重和扩展。



我的要求是让Datagrid选择项目作为ListView中的Multiple SelectionMode。这意味着:

- 您无需按Ctrl键即可选择多个项目。

- 在DataGrid中拖动鼠标时未选择多个项目。



是的,我知道我可以使用ListView并将其设置为所需的Datagrid样式,但已经完成了太多的工作(样式,事件设置器,DataGridCheckBoxColumns,DataGridTemplateColumns等等) 。



有没有人解决过这个问题?无论是使用代码还是扩展Datagrid。

解决方案

来自网络的不同来源,这终于奏效了:



在XAML中:



 <   DataGrid.RowStyle  >  
< 样式 TargetType = {x:输入DataGridRow} >
< EventSetter 事件 = PreviewMouseDown 处理程序 = DataGridRowPreviewMouseDownHandler > < / EventSetter >
< / Style >
< / DataGrid.RowStyle >





在代码隐藏中:

  private   void  DataGridRowPreviewMouseDownHandler( object  sender,MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var row = GetVisualParentByType((FrameworkElement)e.OriginalSource, typeof (DataGridRow)) as DataGridRow;
if (row!= null
{
row。 IsSelected =!row.IsSelected;
e.Handled = true ;
}
}
}

public static DependencyObject GetVisualParentByType(DependencyObject startObject,Type type)
{
DependencyObject parent = startObject;
while (parent!= null
{
if (type.IsInstanceOfType(parent))
break ;
else
parent = VisualTreeHelper.GetParent(parent);
}

return parent;
}


这不是什么大问题,你可以在这里阅读ListView和A DataGridView之间的差异:

WPF DataGrid实际示例 [ ^ ]



以及如何在这里做多选:

http://stackoverflow.com/questions / 2615271 / wpf-datagrid-multiselect-binding [ ^ ]



所以,不用担心? : - )


http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectionmode(v = VS.95).aspx [ ^

For WPF DataGrid, you have 2 Selection Modes: Single and Extended.
For WPF ListView, you have 3 Selection Modes: Single, Multiple and Extended.

My requirement is to get the Datagrid to select items as the Multiple SelectionMode in ListView. This means:
- you select multiple items without pressing Ctrl and
- multiple items are not selected while dragging mouse inside DataGrid.

Yeah, I know I can use the ListView and style it to look like the desired Datagrid, but there''s too much work done already on it (styles, event setters, DataGridCheckBoxColumns, DataGridTemplateColumns and lots more).

Has anyone solved this before? No matter if it''s using code behind or extending Datagrid.

解决方案

From different sources from the web, this finally worked:

In XAML:

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <EventSetter Event="PreviewMouseDown" Handler="DataGridRowPreviewMouseDownHandler"></EventSetter>
    </Style>
</DataGrid.RowStyle>



In code-behind:

private void DataGridRowPreviewMouseDownHandler(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                var row = GetVisualParentByType((FrameworkElement)e.OriginalSource, typeof(DataGridRow)) as DataGridRow;
                if (row != null)
                {
                    row.IsSelected = !row.IsSelected;
                    e.Handled = true;
                }
            }
        }

        public static DependencyObject GetVisualParentByType(DependencyObject startObject, Type type)
        {
            DependencyObject parent = startObject;
            while (parent != null)
            {
                if (type.IsInstanceOfType(parent))
                    break;
                else
                    parent = VisualTreeHelper.GetParent(parent);
            }

            return parent;
        }


Shouldnt be much of an issue, you could read about the differences between a ListView and A DataGridView here:
WPF DataGrid Practical Examples[^]

And how to do multiselect here:
http://stackoverflow.com/questions/2615271/wpf-datagrid-multiselect-binding[^]

So, no worries? :-)


http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectionmode(v=VS.95).aspx[^]


这篇关于具有SelectionMode =“多个”的WPF DataGrid作为ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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