【发布时间】:2016-07-05 09:35:21
【问题描述】:
我有一个 WPF 数据网格,其中添加了将列标题拖到列表框以根据以前的帖子 Drag DataGrid Column header and drop onto a label? 进行分组的功能。我遇到的一个问题是,当调用 DoDragDrop 函数时,它会禁用 Datagrid 的单击排序功能。有没有办法拖放列标题进行分组,但仍然可以点击排序?
private void DataGridHeader_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var header = e.OriginalSource as ContentControl;
if (header != null)
{
DragDrop.DoDragDrop(header, new DataObject(typeof(string), header.Content.ToString()), DragDropEffects.Move);
e.Handled = true;
}
}
}
private void ListBox_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(string)))
{
var columnName = (string)e.Data.GetData(typeof(string));
this.GroupedColumns.Add(columnName);
var sourceView = CollectionViewSource.GetDefaultView(this.DataGrid.ItemsSource);
sourceView.GroupDescriptions.Add(new PropertyGroupDescription(columnName));
sourceView.Refresh();
e.Handled = true;
}
}
我正在通过 XAML 连接事件
ListBox Name="HeaderListBox" VerticalContentAlignment="Center" ItemsSource="{Binding GroupedColumns}" AllowDrop="True" Margin="5" Drop="ListBox_Drop"
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<EventSetter Event="PreviewMouseMove" Handler="DataGridHeader_PreviewMouseMove"/>
</Style>
</DataGrid.ColumnHeaderStyle>
【问题讨论】:
-
您应该添加一些代码来显示您尝试过的内容
-
在列定义中添加 SortMemberPath 可能会解决此问题,但我无法确定,因为您可能没有明确定义列或其他一些可能的问题
-
谢谢戈登。我的代码实现了 DataGridHeader_PreviewMouseMove 和 ListBox_Drop 方法调用,如我的描述中的链接所示。分组工作得很好,但是当我单击列标题时它会禁用排序。我没有定义我的列,我正在设置 DataGrid.ItemsSource = DataTable.AsDataView() 并自动生成列。
标签: wpf sorting datagrid columnheader