ValidationRule 中的 wpf 绑定属性
我有一个带有 2 个文本框的表单:
TotalLoginsTextBox
UploadsLoginsTextBox
我想限制 UploadsLoginsTextBox,因此文本的最大输入将是 TotalLoginsTextBox 的值。 我还使用值转换器,因此我尝试限制最大值:
这是 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>
问题我收到以下错误:
无法在类型的“Maximum”属性上设置“Binding” 'MinMaxRangeValidatorRule'。 “绑定”只能设置在 DependencyObject 的 DependencyProperty。
进行绑定的正确方法是什么?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看到此错误的原因是,如果要将 MinMaxRangeValidatorRule.Maximum 绑定到 MaxLogins,则它必须是 DependencyProperty,而它可能是一个简单的 CLR 属性。
真正的问题是 MinMaxRangeValidatorRule 应该能够从 ValidationRule 和 DependencyObject 继承(以使依赖属性可用)。这在 C# 中是不可能的。
我以这种方式解决了类似的问题:
为验证器规则命名
在代码后面,每当 MaxLogins 更改时设置最大值
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
in code behind, set the Maximum value whenever MaxLogins changes
我猜“MinMaxRangeValidatorRule”是自定义的。
错误消息实际上非常明确,您需要将“Maximum”变量设置为依赖属性,如下所示:
您可以通过在 vs2010 中键入“propdp”来访问依赖属性片段。
I'm guessing the "MinMaxRangeValidatorRule" is something custom.
The error message is quite explicit actually, you need to make the "Maximum" variable a Dependency Property, like so:
You can access the dependency property snippet by typing "propdp" in vs2010.