如何拖放 DataGrid 列标题?

发布于 2024-10-10 19:23:16 字数 143 浏览 2 评论 0原文

我正在使用 WPF DataGrid。我必须拖动列标题,将其放到其他控件(例如标签)上并执行一些操作。但我无法实现 DataGrid 列标题的拖放。我尝试过 ColumnHeaderDragStarted 事件,但无法在处理程序中找到列标题对象或标题名称。 有什么帮助吗?

I am working with WPF DataGrid. I have to drag the Column Header, drop it to some other control(say Label) and do some operation. But i am not able to achieve drag and drop of DataGrid Column Header. I have tried with ColumnHeaderDragStarted event, but I am not able to find Column Header object or just the Name of header in the handler.
Any help plz!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很糊涂小朋友 2024-10-17 19:23:16

也许这可以帮助您:

在 XAML 上:

在 C# 代码上:

    private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
    {
        DependencyObject dep = (DependencyObject)e.OriginalSource;

        while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }

        if (dep == null)
            return;

        if (dep is DataGridColumnHeader)
        {
            DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;

            // find the property that this cell's column is bound to
            string boundPropertyName = FindBoundProperty(columnHeader.Column);

            int columnIndex = columnHeader.Column.DisplayIndex;

            ClickedItemDisplay.Text = string.Format(
                "Header clicked [{0}] = {1}",
                columnIndex, boundPropertyName);
        }

        if (dep is DataGridCell)
        {
            DataGridCell cell = dep as DataGridCell;

            // navigate further up the tree
            while ((dep != null) && !(dep is DataGridRow))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep == null)
                return;

            DataGridRow row = dep as DataGridRow;

            object value = ExtractBoundValue(row, cell);

            int columnIndex = cell.Column.DisplayIndex;
            int rowIndex = FindRowIndex(row);

            ClickedItemDisplay.Text = string.Format(
                "Cell clicked [{0}, {1}] = {2}",
                rowIndex, columnIndex, value.ToString());
        }
    }

    /// <summary>
    /// Determine the index of a DataGridRow
    /// </summary>
    /// <param name="row"></param>
    /// <returns></returns>
    private int FindRowIndex(DataGridRow row)
    {
        DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer(row) as DataGrid;

        int index = dataGrid.ItemContainerGenerator.IndexFromContainer(row);

        return index;
    }

    /// <summary>
    /// Find the value that is bound to a DataGridCell
    /// </summary>
    /// <param name="row"></param>
    /// <param name="cell"></param>
    /// <returns></returns>
    private object ExtractBoundValue(DataGridRow row, DataGridCell cell)
    {
        // find the property that this cell's column is bound to
        string boundPropertyName = FindBoundProperty(cell.Column);

        // find the object that is realted to this row
        object data = row.Item;

        // extract the property value
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(data);
        PropertyDescriptor property = properties[boundPropertyName];
        object value = property.GetValue(data);

        return value;
    }

    /// <summary>
    /// Find the name of the property which is bound to the given column
    /// </summary>
    /// <param name="col"></param>
    /// <returns></returns>
    private string FindBoundProperty(DataGridColumn col)
    {
        DataGridBoundColumn boundColumn = col as DataGridBoundColumn;

        // find the property that this column is bound to
        Binding binding = boundColumn.Binding as Binding;
        string boundPropertyName = binding.Path.Path;

        return boundPropertyName;
    }
}

// This XAML and C# where extracted from a link contained on this URL:    
//    http://social.msdn.microsoft.com/Forums/en/wpf/thread/61707b8a-e6c6-474b-ac2b-3446319625bd

Maybe this can help you:

On XAML:

<DataGrid Name="DataGrid" MouseRightButtonUp="DataGrid_MouseRightButtonUp" />

On C# Code:

    private void DataGrid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
    {
        DependencyObject dep = (DependencyObject)e.OriginalSource;

        while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
        {
            dep = VisualTreeHelper.GetParent(dep);
        }

        if (dep == null)
            return;

        if (dep is DataGridColumnHeader)
        {
            DataGridColumnHeader columnHeader = dep as DataGridColumnHeader;

            // find the property that this cell's column is bound to
            string boundPropertyName = FindBoundProperty(columnHeader.Column);

            int columnIndex = columnHeader.Column.DisplayIndex;

            ClickedItemDisplay.Text = string.Format(
                "Header clicked [{0}] = {1}",
                columnIndex, boundPropertyName);
        }

        if (dep is DataGridCell)
        {
            DataGridCell cell = dep as DataGridCell;

            // navigate further up the tree
            while ((dep != null) && !(dep is DataGridRow))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep == null)
                return;

            DataGridRow row = dep as DataGridRow;

            object value = ExtractBoundValue(row, cell);

            int columnIndex = cell.Column.DisplayIndex;
            int rowIndex = FindRowIndex(row);

            ClickedItemDisplay.Text = string.Format(
                "Cell clicked [{0}, {1}] = {2}",
                rowIndex, columnIndex, value.ToString());
        }
    }

    /// <summary>
    /// Determine the index of a DataGridRow
    /// </summary>
    /// <param name="row"></param>
    /// <returns></returns>
    private int FindRowIndex(DataGridRow row)
    {
        DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer(row) as DataGrid;

        int index = dataGrid.ItemContainerGenerator.IndexFromContainer(row);

        return index;
    }

    /// <summary>
    /// Find the value that is bound to a DataGridCell
    /// </summary>
    /// <param name="row"></param>
    /// <param name="cell"></param>
    /// <returns></returns>
    private object ExtractBoundValue(DataGridRow row, DataGridCell cell)
    {
        // find the property that this cell's column is bound to
        string boundPropertyName = FindBoundProperty(cell.Column);

        // find the object that is realted to this row
        object data = row.Item;

        // extract the property value
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(data);
        PropertyDescriptor property = properties[boundPropertyName];
        object value = property.GetValue(data);

        return value;
    }

    /// <summary>
    /// Find the name of the property which is bound to the given column
    /// </summary>
    /// <param name="col"></param>
    /// <returns></returns>
    private string FindBoundProperty(DataGridColumn col)
    {
        DataGridBoundColumn boundColumn = col as DataGridBoundColumn;

        // find the property that this column is bound to
        Binding binding = boundColumn.Binding as Binding;
        string boundPropertyName = binding.Path.Path;

        return boundPropertyName;
    }
}

// This XAML and C# where extracted from a link contained on this URL:    
//    http://social.msdn.microsoft.com/Forums/en/wpf/thread/61707b8a-e6c6-474b-ac2b-3446319625bd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文