gpt4 book ai didi

wpf - {RelativeSource PreviousData} 与 DataGrid 一起使用时始终为 NULL

转载 作者:行者123 更新时间:2023-12-01 06:26:25 24 4
gpt4 key购买 nike

我想将 {RelativeSource PreviousData} 与 Datagrid 一起使用。我想在转换器中使用该值。但是对于 DataGrid,它总是给出空值。而它与 ListBox 完美配合。我们还需要什么来使它与 DataGrid 一起工作吗?

最佳答案

DataGridTemplateColumn 不属于 DataGrid 的可视树或逻辑树。这可以根据 Thomas Levesque 提出的解决方案使用 Freezable 类来解决。 .

public class BindingProxy : Freezable
{

protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}


public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}

// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}

在 DataGrid 的资源中声明该类的一个实例,并将 Data 属性绑定(bind)到当前 DataContext:
<DataGrid.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>

最后一步是将此 BindingProxy 对象指定为绑定(bind)的 Source。

关于wpf - {RelativeSource PreviousData} 与 DataGrid 一起使用时始终为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28104446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com