[英]Dependency Property not updating
I'm trying to add parameters to my custom validation rule. 我正在尝试向自定义验证规则添加参数。 For this I defined a dependency object like this:
为此,我定义了一个依赖对象,如下所示:
public class SettingsValueValidationDependencyObject : DependencyObject
{
public Custom.ValueType ValueTypeForValidation
{
get { return (Custom.ValueType)this.GetValue(ValueTypeForValidationProperty); }
set { this.SetValue(ValueTypeForValidationProperty, value); }
}
public static readonly DependencyProperty ValueTypeForValidationProperty = DependencyProperty.Register("ValueTypeForValidation", typeof(Custom.ValueType), typeof(SettingsValueValidationDependencyObject), new UIPropertyMetadata(Custom.ValueType.Int32Value));
}
My validation rule class looks like this: 我的验证规则类如下所示:
public class SettingsValueValidationRule : ValidationRule
{
public SettingsValueValidationDependencyObject SettingsValueValidationDependencyObject
{
get;
set;
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
// validation...
}
}
xaml code: XAML代码:
<DataGridTextColumn Header="Value" Width="150">
<DataGridTextColumn.Binding>
<Binding Path="Value">
<Binding.ValidationRules>
<validators:SettingsValueValidationRule>
<validators:SettingsValueValidationRule.SettingsValueValidationDependencyObject>
<validators:SettingsValueValidationDependencyObject ValueTypeForValidation="{Binding ValueType}"/>
</validators:SettingsValueValidationRule.SettingsValueValidationDependencyObject>
</validators:SettingsValueValidationRule>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
The two properties Value and ValueType both belong to the same object and the DataGrid's ItemsSource is bound to a list of these object. Value和ValueType这两个属性都属于同一个对象,DataGrid的ItemsSource绑定到这些对象的列表。 When I edit the Value cell, the ValueTypeForValidation property is always the default value (I also have a column to display the ValueType and its definitely another value).
当我编辑“值”单元格时,ValueTypeForValidation属性始终是默认值(我也有一列显示ValueType及其绝对是另一个值)。 I also tried to update the BindingExpression manually in the Validate method but it won't work.
我也尝试在Validate方法中手动更新BindingExpression,但无法正常工作。 What am I doing wrong?
我究竟做错了什么?
There is no Binding in ValidationRules. ValidationRules中没有绑定。
ValidationRules are not part of LogicalTree and so there is no DataContext to serve as Source in your Binding. ValidationRules不是LogicalTree的一部分,因此没有DataContext可以用作绑定中的Source。
There are however few tricks on the internet how to make a ValidationRule "bindable". 但是,互联网上很少有技巧可以使ValidationRule“可绑定”。
Take a look at this tut: 看一下这个tut:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.