【问题标题】:Paste Functionality for WPF DataGrid with DataGridTemplateColumns使用 DataGridTemplateColumns 粘贴 WPF DataGrid 的功能
【发布时间】:2011-10-10 10:09:51
【问题描述】:

我最近开始将 WPF Datagrid 与包含 WPF AutoCompleteBox 的 DataGridTemplateColumns 一起使用,但在为这些 DataGridTemplateColumns 实现 Clipboard.Paste 功能时遇到了麻烦。

我已经通过 Vishal 的指南 here 设法让 Clipboard.Paste 与内置 DataGridColumns 一起使用,但它不适用于 DataGridTemplateColumns。

深入研究 DataGridColumn 类中的 OnPastingCellClipboardContent 方法,似乎 fe.GetBindingExpression(CellValueProperty) 返回 null 而不是所需的 BindingExpression。

谁能指出我正确的方向?

public virtual void OnPastingCellClipboardContent(object item, object cellContent)
    {
        BindingBase binding = ClipboardContentBinding;
        if (binding != null)
        {
            // Raise the event to give a chance for external listeners to modify the cell content
            // before it gets stored into the cell
            if (PastingCellClipboardContent != null)
            {
                DataGridCellClipboardEventArgs args = new DataGridCellClipboardEventArgs(item, this, cellContent);
                PastingCellClipboardContent(this, args);
                cellContent = args.Content;
            }

            // Event handlers can cancel Paste of a cell by setting its content to null
            if (cellContent != null)
            {
                FrameworkElement fe = new FrameworkElement();
                fe.DataContext = item;
                fe.SetBinding(CellValueProperty, binding);
                fe.SetValue(CellValueProperty, cellContent);

                BindingExpression be = fe.GetBindingExpression(CellValueProperty);

        be.UpdateSource();

    }

}

谢谢!

【问题讨论】:

    标签: c# wpf wpfdatagrid


    【解决方案1】:

    使用 ClipboardContentBinding 并将绑定的模式设置为 TwoWay,似乎可行。

    GetBindingExpression 然后返回非空值(ClipboardContentBinding 上的绑定)并且 UpdateSource 不会失败。

    我想这个解决方案仅限于在源上触发 PropertyChanged 事件的情况,这反过来会更新列的 DataTemplate 中的控件。

    【讨论】:

      【解决方案2】:

      这是因为 DataGridTemplateColumns 没有绑定。绑定在您的数据模板中处理。单元格数据模板只是获取项目(行中的项目)并绑定到它。列无法知道单元格中的内容。

      我通过创建自己的专栏来解决这个问题。我从 DataGridTextColumn 派生(如果我正在做一个有文本输入的)并覆盖 GenerateElement 和 GenerateEditingElement。

      这样我仍然拥有可用于粘贴的绑定属性。

      【讨论】:

        【解决方案3】:

        如图所示使用ClipboardContentBinding

        <DataGridTemplateColumn 
            Header="First Name" 
            SortMemberPath="FirstName" 
            ClipboardContentBinding="{Binding FirstName}" 
            >
            <DatGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FirstName}" />
                </DataTemplate>
            </DatGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                ...
            </DataGridTemplateColumn>
        </DataGridTemplateColumn>
        

        取自here

        【讨论】:

        • 这不适用于 WPFToolkit 2010 年 2 月版以及可能的其他版本。
        • 这是在某处记录的,还是您通过测试发现的? ClipboardContentBinding 作为属性存在于 DataGridColumn.cs
        • 我知道它存在,并且我已将错误跟踪到 OP。如果您仔细查看 OP 发布的代码,那么您会意识到虽然 ClipboardContentBinding 正在使用,但它会失败。我已经通过反射编写了一个解决方法,因为我不想创建自定义列。简而言之:不,不适用于 WPFToolkit 的模板列。不过,我不了解带有 .NET 4 的 WPF。
        猜你喜欢
        • 2011-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-07
        • 2011-10-19
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多