拖动DataGrid列标题并放到标签上? [英] Drag DataGrid Column header and drop onto a label?

查看:197
本文介绍了拖动DataGrid列标题并放到标签上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在现有的WPF DataGrid中添加一个drop区域,在列标题之上。在该区域,我想允许用户删除表示要分组的列的列标题,并通过从这个区域中删除这些列来取消分组。我无法拖动瓷砖,然后放在标签上。我的方法包括检测拖动作为MouseMove和MouseLeftButtonDown的组合,但它不工作。示例代码:

I want to add a "drop" area to the existing WPF DataGrid, above the column headers. In that area I want to allow the user to drop column headers that represent the columns to be grouped and to ungroup those columns by removing them from this area. I'm not able to drag the tiles and then drop onto a label. My approach consisted of Detecting drag as a combination of MouseMove and MouseLeftButtonDown but it din't work. Sample Code:

    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.            
    }

我要删除此按钮并引入列名称的拖放(表)放在标签上。欢迎任何建议。谢谢!

I want to remove this Button and introduce a Drag-Drop of the Column name ("Table")onto a label. Any suggestions are welcomed. Thanks!

推荐答案

你应该做的最小的事情:

Minimal things you should do:

1)重新设计 DataGridColumnHeader 添加 PreviewMouseMove 事件处理程序。

2)在事件处理程序中,启动拖放操作。

3)将 GroupStyle 添加到 DataGrid

4)对于要删除目标的元素,处理删除事件并更新 GroupDescriptions

1) Re-style DataGridColumnHeader to add PreviewMouseMove event handler.
2) In event handler, initiate drag/drop operation.
3) Add GroupStyle to DataGrid.
4) For the element, which is drop target, handle Drop event and update 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>

代码(假设列标题只包含字符串):

Code (assumes, that column header contains only strings):

    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;
        }
    }

取消分组是相似的。

这篇关于拖动DataGrid列标题并放到标签上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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