如何使 AttachedProperty 成为多重绑定的目标?
我一直在使用 WPF DataGrid 并尝试集中我的单元格样式。 在这次重构过程中,我发现我的单元格样式需要了解每列不同的验证逻辑。 我决定在我的列对象上提供一个附加属性,该属性将包含我的验证逻辑的结果(每列的逻辑不同)并且可以在我的 DataGridCell 样式中访问。 不幸的是,我绑定到附加属性的多重绑定不起作用。
我的单元格样式包括一个 DataTrigger,其中触发器的绑定路径是一个附加属性。 (请注意,样式的 TargetType 是 DataGridCell,它具有 Column 属性)
<DataTrigger Value="Error">
<DataTrigger.Binding>
<Binding Converter="{StaticResource debugConverter}"
RelativeSource="{RelativeSource Self}"
Path="Column.ValidationValue" Mode="OneWay" />
</DataTrigger.Binding>
<Setter Property="BorderBrush" Value="{StaticResource errorBrush}" />
</DataTrigger>
我已在 DataGrid 类(名为 ValidatingDataGrid 并扩展 DataGrid)中定义了附加属性,如下所示:
public static readonly DependencyProperty ValidationValueProperty =
DependencyProperty.RegisterAttached("ValidationValue", typeof(object),
typeof(DataGridColumn));
public static object GetValidationValue(DependencyObject element)
{
return element.GetValue(ValidationValueProperty);
}
public static void SetValidationValue(DependencyObject element, object value)
{
element.SetValue(ValidationValueProperty, value);
}
最后,在我的 WPF 页面中,我有一个 DataGridTextColumn ,我尝试将 ValidationValue (列的 AttachedProperty)绑定到 MultiBinding。
<vfc:ValidatingDataGrid>
<vfc:ValidatingDataGrid.Columns>
<tk:DataGridTextColumn Header="Name" Width="1.5*">
<tk:DataGridTextColumn.Binding>
<Binding Path="Name" />
</tk:DataGridTextColumn.Binding>
<vfc:ValidatingDataGrid.ValidationValue>
<MultiBinding Converter="{StaticResource validityConverter}"
ConverterParameter="Name">
<Binding Mode="OneWay" />
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged" />
</MultiBinding>
</vfc:ValidatingDataGrid.ValidationValue>
然而,当我尝试运行此程序时,我始终收到 XAML Parse Exception:
发生 System.Windows.Markup.XamlParseException
Message="无法在“SetValidationValue”上设置“MultiBinding” “DataGridTextColumn”类型的属性。 “多重绑定”只能是 设置 DependencyObject 的 DependencyProperty。”
来源=“演示框架”
行号=0
线位置=0
堆栈跟踪: 在 MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension markupExtension,IProvideValueTarget 提供ValueTarget, 依赖对象& targetDependencyObject、DependencyProperty& 目标依赖属性)
内部异常:空
我知道如果我将 ValidationValue 设置为静态值(例如 Error),该值将在 DataTrigger 中正确存储和访问。
谁能解释一下问题到底是什么? 我不明白异常消息的含义,因为 AttachedProperty 是 DependencyProperty,并且 DataGrid 和 DataGridColumn 都是 DependencyObject。
有没有办法将 ValidationValue AttachedProperty 绑定到 MultiBinding? 如果没有,是否有其他一些 WPF 机制可以让我存储绑定结果,以便我的 DataGridCell 样式可以读取它?
I've been working with the WPF DataGrid and trying to centralize my cell style. During this refactor, I came across a need for my cell style to know validation logic that is different for each column. I decided to provide an attached property on my column object, which would contain the result of my validation logic (with the logic being different per column) and be accessable in my DataGridCell style. Unfortunately, the MultiBinding that I've tied to the Attached Property doesn't work.
My cell style includes a DataTrigger, where the trigger's binding path is an Attached Property. (Note that the style's TargetType is DataGridCell, which has a Column property)
<DataTrigger Value="Error">
<DataTrigger.Binding>
<Binding Converter="{StaticResource debugConverter}"
RelativeSource="{RelativeSource Self}"
Path="Column.ValidationValue" Mode="OneWay" />
</DataTrigger.Binding>
<Setter Property="BorderBrush" Value="{StaticResource errorBrush}" />
</DataTrigger>
I've defined the Attached Property in my DataGrid class (which is named ValidatingDataGrid and extends the DataGrid) as follows:
public static readonly DependencyProperty ValidationValueProperty =
DependencyProperty.RegisterAttached("ValidationValue", typeof(object),
typeof(DataGridColumn));
public static object GetValidationValue(DependencyObject element)
{
return element.GetValue(ValidationValueProperty);
}
public static void SetValidationValue(DependencyObject element, object value)
{
element.SetValue(ValidationValueProperty, value);
}
Finally, in my WPF Page, I have a DataGridTextColumn, where I attempt to bind ValidationValue (the Column's AttachedProperty) to a MultiBinding.
<vfc:ValidatingDataGrid>
<vfc:ValidatingDataGrid.Columns>
<tk:DataGridTextColumn Header="Name" Width="1.5*">
<tk:DataGridTextColumn.Binding>
<Binding Path="Name" />
</tk:DataGridTextColumn.Binding>
<vfc:ValidatingDataGrid.ValidationValue>
<MultiBinding Converter="{StaticResource validityConverter}"
ConverterParameter="Name">
<Binding Mode="OneWay" />
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged" />
</MultiBinding>
</vfc:ValidatingDataGrid.ValidationValue>
When I try to run this, however, I consistently get a XAML Parse Exception:
System.Windows.Markup.XamlParseException occurred
Message="A 'MultiBinding' cannot be set on the 'SetValidationValue'
property of type 'DataGridTextColumn'. A 'MultiBinding' can only be
set on a DependencyProperty of a DependencyObject."Source="PresentationFramework"
LineNumber=0
LinePosition=0
StackTrace:
at MS.Internal.Helper.CheckCanReceiveMarkupExtension(MarkupExtension
markupExtension, IProvideValueTarget provideValueTarget,
DependencyObject& targetDependencyObject, DependencyProperty&
targetDependencyProperty)InnerException: Null
I know that if I set ValidationValue to a static value (for example, Error), the value is properly stored and accessed in the DataTrigger.
Can anyone explain what the problem actually is? I don't understand what the exception message means since the AttachedProperty is a DependencyProperty, and DataGrid and DataGridColumn are both DependencyObjects.
Is there a way to bind the ValidationValue AttachedProperty to the MultiBinding? If not, is there some other WPF mechanism that will let me store the result of a binding so my DataGridCell style can read it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚遇到了同样的问题,并且出现了同样令人困惑的异常。 您需要将依赖属性的所有者类型设置为具有 Setxxx 和 Getxxx 方法的类,而不是要使用该属性的类。 如果方法位于 ValidatingDataGrid 中,您应该像这样注册属性:
I just ran in to the same problem with the same confusing exception. You need to set the owner type of the depenency property to be the class that has the Setxxx and Getxxx methods, not the class that you want to use the property on. If the methods are in ValidatingDataGrid, you should register the property like this: