使用 DataGridTemplateColumns 的 WPF DataGrid 粘贴功能
我最近开始使用 WPF Datagrid 和包含 WPF AutoCompleteBox 的 DataGridTemplateColumns,但我在为这些 DataGridTemplateColumns 实现 Clipboard.Paste 功能时发现了麻烦。
我已成功通过 Vishal 的指南让 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();
}
}
谢谢!
I recently started using the WPF Datagrid with DataGridTemplateColumns containing the WPF AutoCompleteBox, but I'm finding trouble in implementing Clipboard.Paste functionality for these DataGridTemplateColumns.
I've managed to get Clipboard.Paste working with built-in DataGridColumns via Vishal's guide here, but it doesn't work with DataGridTemplateColumns.
Delving into the OnPastingCellClipboardContent method in the DataGridColumn class, it appears that fe.GetBindingExpression(CellValueProperty) is returning null rather than the required BindingExpression.
Can anyone point me to the right direction?
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();
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 ClipboardContentBinding 并将绑定的模式设置为 TwoWay 似乎有效。
然后,GetBindingExpression 返回不为 null 的内容(ClipboardContentBinding 上的绑定),并且 UpdateSource 不会失败。
我想这个解决方案仅限于在源上触发 PropertyChanged 事件的情况,该事件又更新列的 DataTemplate 中的控件。
Using ClipboardContentBinding and setting the binding's Mode to TwoWay, seems to works.
GetBindingExpression then returns something not null (the binding on ClipboardContentBinding) and the UpdateSource does not fail.
I imagine this solution is limited to the case where you have a PropertyChanged event triggered on the source, which in turn update the control in the column's DataTemplate.
这是因为 DataGridTemplateColumns 没有绑定。绑定在您的数据模板中进行。单元格数据模板只是获取项目(行中的项目)并绑定到它。列无法知道单元格中的内容。
我通过创建自己的专栏来解决这个问题。我从 DataGridTextColumn 派生(如果我正在做一个有文本输入的列)并覆盖GenerateElement 和GenerateEditingElement。
这样我仍然拥有可用于粘贴的绑定属性。
This is because for DataGridTemplateColumns there isnt a binding. The binding is taken care of in your datatemplate. The cell datatemplate just gets the item (the item in the row) and binds to it. there is no way for the column to know what is in the cell.
I have worked around this by creating my own columns. I derive from DataGridTextColumn (if i am doing one that has text input) and override the GenerateElement and GenerateEditingElement.
This way i still have the binding property that can be used for pasting.
使用
ClipboardContentBinding
如下所示:取自 这里。
Use
ClipboardContentBinding
as shown:Taken from here.