我想在列标题上方的现有WPF DataGrid中添加“drop”区域。在该区域中,我希望允许用户删除表示要分组的列的列标题,并通过从这个区域中删除它们来取消组合这些列。我无法拖动瓷砖然后掉到标签上。我的方法包括检测拖动作为MouseMove和MouseLeftButtonDown的组合,但它无法正常工作。示例代码:
DataTable dt = new DataTable();
dt.Columns.Add("Table", typeof(string));
dt.Columns.Add("Scan Count", typeof(string));
DataRow dr = dt.NewRow();
dr["Scan Count"] = sum.ToString();
dr["logical reads"] = sum1.ToString();
private void Button_Click(object sender, RoutedEventArgs e)
{
ICollectionView cvTasks = CollectionViewSource.GetDefaultView(GridView1.ItemsSource);
if (cvTasks != null && cvTasks.CanGroup == true)
{
cvTasks.GroupDescriptions.Clear();
cvTasks.GroupDescriptions.Add(new PropertyGroupDescription("Table"));
}
}
public class Tasks : ObservableCollection<Task>
{
//Creating the Tasks collection in this way enables data binding from XAML.
}
我想删除此Button并将列名称(“Table”)拖放到标签上。欢迎任何建议。谢谢!
答案 0 :(得分:4)
你应该做的最少的事情:
1)重新设置DataGridColumnHeader
样式以添加PreviewMouseMove
事件处理程序
2)在事件处理程序中,启动拖放操作
3)将GroupStyle
添加到DataGrid
4)对于作为放置目标的元素,处理Drop
事件并更新GroupDescriptions
。
XAML:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<ListBox x:Name="MyGroupDescriptionsList" AllowDrop="True" Drop="ListBox_Drop"/>
<DataGrid x:Name="MyDataGrid" Grid.Row="1" ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<EventSetter Event="PreviewMouseMove" Handler="DataGridHeader_PreviewMouseMove"/>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Header="{Binding Name}">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="A" Binding="{Binding A}"/>
<DataGridTextColumn Header="B" Binding="{Binding B}"/>
<DataGridTextColumn Header="C" Binding="{Binding C}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
代码(假设,列标题只包含字符串):
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));
MyGroupDescriptionsList.Items.Add(columnName);
var sourceView = CollectionViewSource.GetDefaultView(MyDataGrid.ItemsSource);
sourceView.GroupDescriptions.Add(new PropertyGroupDescription(columnName));
sourceView.Refresh();
e.Handled = true;
}
}
取消分组是相似的。