我想在DataGrid中使用{RelativeSource PreviousData},并将该值用于转换器。但是,在DataGrid中它总是给出null值。然而,在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.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>