WPF在有效性规则绑定属性 [英] wpf binding property in ValidationRule
问题描述
我遇到了两个文本框形式:
TotalLoginsTextBox
UploadsLoginsTextBox
块引用>我想限制UploadsLoginsTextBox这样的文本的最大输入将是TotalLoginsTextBox的值。
我还使用值转换器,所以我尝试绑定的最大值:这是XAML:
<! - 合计登录 - >
<标签保证金=5个总:LT; /标签>
<文本框名称=TotalLoginsTextBox了minWidth =30文本={绑定路径= MAXLOGINS,模式=双向}/>
<! - 上传 - >
<标签保证金=5>上传:LT; /标签>
&所述;文本框名称=UploadsLoginsTextBox了minWidth =30>
< TextBox.Text>
<绑定路径=MAXUP模式=双向NotifyOnValidationError =真>
< Binding.ValidationRules>
<校验:MinMaxRangeValidatorRule最小=0最大={绑定路径= MAXLOGINS}/>
< /Binding.ValidationRules>
< /装订>
< /TextBox.Text>
< /文本框>问题我收到以下错误:
一个绑定不能在类型的'最大'属性设置
MinMaxRangeValidatorRule。 A'绑定'只能在一组
的DependencyProperty DependencyObject的。
块引用>什么是做绑定的正确方法?
解决方案您会看到这个错误的原因的 MinMaxRangeValidatorRule.Maximum 的需要是一个的DependencyProperty,如果你想将其绑定到的 MAXLOGINS 的,虽然它可能是一个简单的CLR属性。
真正的问题是,MinMaxRangeValidatorRule应该能够从有效性规则和自DependencyObject(使依赖属性可用)继承。这是不可能的在C#
我解决这样一个类似的问题:
提供一个名称的验证规则
<校验:MinMaxRangeValidatorRule NAME =MinMaxValidator最小=0/>
在code的背后,设置最大值时MAXLOGINS变化
公众诠释MAXLOGINS
{
{返回(INT)的GetValue(MaxLoginsProperty); }
集合{的SetValue(MaxLoginsProperty,值); }
}
公共静态的DependencyProperty MaxLoginsProperty = DependencyProperty.Register(MAXLOGINS
typeof运算(INT)
typeof运算(mycontrol)
新PropertyMetadata(HandleMaxLoginsChanged));私有静态无效HandleMinValueChanged(DependencyObject的D,DependencyPropertyChangedEventArgs E)
{
mycontrol源=(mycontrol)D;
source.MinMaxValidator.Maximum =(INT)e.NewValue;
}
i'm having a form with 2 text boxes:
TotalLoginsTextBox
UploadsLoginsTextBox
i want to limit UploadsLoginsTextBox so the maximum input for the text will be the value of the TotalLoginsTextBox. i am also using a value converter so i try to bound the Maximum value:
this is the XAML:
<!-- Total Logins --> <Label Margin="5">Total:</Label> <TextBox Name="TotalLoginsTextBox" MinWidth="30" Text="{Binding Path=MaxLogins, Mode=TwoWay}" /> <!-- Uploads --> <Label Margin="5">Uploads:</Label> <TextBox Name="UploadsLoginsTextBox" MinWidth="30"> <TextBox.Text> <Binding Path="MaxUp" Mode="TwoWay" NotifyOnValidationError="True"> <Binding.ValidationRules> <Validators:MinMaxRangeValidatorRule Minimum="0" Maximum="{Binding Path=MaxLogins}" /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox>
the problem i am getting the following error:
A 'Binding' cannot be set on the 'Maximum' property of type 'MinMaxRangeValidatorRule'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
what is the proper way to do the binding ?
解决方案You're seeing this error because MinMaxRangeValidatorRule.Maximum needs to be a DependencyProperty if you want to bind it to MaxLogins, while it is probably a simple CLR property.
The real problem is that MinMaxRangeValidatorRule should be able to inherit from ValidationRule AND from DependencyObject (to make Dependency Properties available). This is not possible in C#.
I solved a similar problem in this way:
give a name to your validator rule
<Validators:MinMaxRangeValidatorRule Name="MinMaxValidator" Minimum="0" />
in code behind, set the Maximum value whenever MaxLogins changes
public int MaxLogins { get { return (int )GetValue(MaxLoginsProperty); } set { SetValue(MaxLoginsProperty, value); } } public static DependencyProperty MaxLoginsProperty = DependencyProperty.Register("MaxLogins ", typeof(int), typeof(mycontrol), new PropertyMetadata(HandleMaxLoginsChanged)); private static void HandleMinValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { mycontrol source = (mycontrol) d; source.MinMaxValidator.Maximum = (int) e.NewValue; }
这篇关于WPF在有效性规则绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!