c# - 如何拖放 DataGrid 列标题?

标签 c# wpf xaml datagrid drag-and-drop

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

最佳答案

也许这可以帮到你:

在 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

关于c# - 如何拖放 DataGrid 列标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4605476/

相关文章:

c# - 如何限制DataGridView中可以选择的行数

c# - mvvmcross 导航问题,出现异常 : Could not find view for [ViewModelName]

c# - 如何将绑定(bind)的 TextBlock 的部分加粗?

c# - 从任何线程捕获未处理的异常

c# - ResourceDictionary WPF 样式中的交互触发器

c# - 以编程方式修改样式内的属性

c# - WinForms Control.BeginInvoke 异步回调

WPF 命令行

wpf - 没有验证错误 WPF 时不显示工具提示

WPF:调整圆的大小,保持中心点而不是 TopLeft?