我需要将绑定值传递给验证规则。这是XAML代码:
<Label Grid.Column="11" Content="Default" Style="{StaticResource labelStyle2}" x:Name="txtTipo" />
<TextBox Grid.Column="12" Style="{StaticResource txtDataStyle1}" Width="100" TextChanged="Data_TextChanged">
<Binding Path="ConfigObject.Edit.Default" UpdateSourceTrigger="Default">
<Binding.ValidationRules>
<local:GenericValidationRule>
<local:GenericValidationRule.Wrapper>
<local:Wrapper TipoInterno="{Binding ElementName=txtTipo, Path=Content}"/>
</local:GenericValidationRule.Wrapper>
</local:GenericValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox>
隐藏代码:
public class GenericValidationRule : ValidationRule
{
public Wrapper Wrapper { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var xx = Wrapper.TipoInternoProperty; //TEST
return new ValidationResult(is_valid, error_context);
}
}
public class Wrapper : DependencyObject
{
public static readonly DependencyProperty TipoInternoProperty = DependencyProperty.Register("TipoInterno", typeof(string), typeof(Wrapper), new PropertyMetadata(string.Empty));
public string TipoInterno
{
get { return (string)GetValue(TipoInternoProperty); }
set { SetValue(TipoInternoProperty, value); }
}
}
基本上,我无法将所需的值添加到我的TinoInterno属性中。如果我将这样的值硬编码:
<local:Wrapper TipoInterno="TEST" />
该属性已正确评估。在第一种情况下,我需要使用elementName txtTipo传递控件的属性Content。 怎么了?
答案 0 :(得分:1)
您应该在TipoInterno
中获得Wrapper
实例的ValidationRule
的值:
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
string s = Wrapper.TipoInterno;
return ...;
}
然后您可以使用x:Reference
进行绑定:
<local:Wrapper TipoInterno="{Binding Path=Content, Source={x:Reference txtTipo}}"/>