ValidationRule 中的 wpf 绑定属性

发布于 2024-12-04 20:21:17 字数 1142 浏览 4 评论 0原文

我有一个带有 2 个文本框的表单:

  1. TotalLoginsTextBox

  2. 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:

  1. TotalLoginsTextBox

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2024-12-11 20:21:17

您看到此错误的原因是,如果要将 MinMaxRangeValidatorRule.Maximum 绑定到 MaxLogins,则它必须是 DependencyProperty,而它可能是一个简单的 CLR 属性。

真正的问题是 MinMaxRangeValidatorRule 应该能够从 ValidationRule 和 DependencyObject 继承(以使依赖属性可用)。这在 C# 中是不可能的。

我以这种方式解决了类似的问题:

  1. 为验证器规则命名

    <验证器:MinMaxRangeValidatorRule Name="MinMaxValidator" 最小值="0" />
    
  2. 在代码后面,每当 MaxLogins 更改时设置最大值

    public int MaxLogins 
    {
        获取 { 返回 (int )GetValue(MaxLoginsProperty); }
        设置 { SetValue(MaxLoginsProperty, 值); }
    }
    公共静态 DependencyProperty MaxLoginsProperty = DependencyProperty.Register("MaxLogins ", 
                                                                                        类型(int), 
                                                                                        类型(我的控件), 
                                                                                        新的 PropertyMetadata(HandleMaxLoginsChanged));
    
    私有静态无效 HandleMinValueChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
    {
        mycontrol 源 = (mycontrol) d;
        source.MinMaxValidator.Maximum = (int) e.NewValue;
    }
    

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:

  1. give a name to your validator rule

    <Validators:MinMaxRangeValidatorRule Name="MinMaxValidator" Minimum="0" />
    
  2. 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;
    }
    
分分钟 2024-12-11 20:21:17

我猜“MinMaxRangeValidatorRule”是自定义的。

错误消息实际上非常明确,您需要将“Maximum”变量设置为依赖属性,如下所示:

public int Maximum
{
    get { return (int)GetValue(MaximumProperty); }
    set { SetValue(MaximumProperty, value); }
}

// Using a DependencyProperty as the backing store for Maximum.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MaximumProperty =
    DependencyProperty.Register("Maximum", typeof(int), typeof(MinMaxRangeValidatorRule), new UIPropertyMetadata(0));

您可以通过在 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:

public int Maximum
{
    get { return (int)GetValue(MaximumProperty); }
    set { SetValue(MaximumProperty, value); }
}

// Using a DependencyProperty as the backing store for Maximum.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty MaximumProperty =
    DependencyProperty.Register("Maximum", typeof(int), typeof(MinMaxRangeValidatorRule), new UIPropertyMetadata(0));

You can access the dependency property snippet by typing "propdp" in vs2010.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文