【问题标题】:wpf binding property in ValidationRuleValidationRule 中的 wpf 绑定属性
【发布时间】:2011-09-17 16:51:18
【问题描述】:

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

  1. TotalLoginsTextBox

  2. 上传登录文本框

我想限制 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>

我收到以下错误的问题:

不能在类型的“最大值”属性上设置“绑定” 'MinMaxRangeValidatorRule'。 “绑定”只能设置在 DependencyObject 的 DependencyProperty。

进行绑定的正确方法是什么?

【问题讨论】:

    标签: wpf binding validation


    【解决方案1】:

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

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

    我用这种方式解决了一个类似的问题:

    1. 为您的验证器规则命名

      <Validators:MinMaxRangeValidatorRule Name="MinMaxValidator" Minimum="0" />
      
    2. 在后面的代码中,在 MaxLogins 更改时设置最大值

      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;
      }
      

    【讨论】:

    • 很好的解决方案,谢谢。我非常专注于让 Binding 工作,以至于我完全忘记了如何简单地克服这个问题
    【解决方案2】:

    我猜“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”来访问依赖属性sn-p。

    【讨论】:

    • 您不能简单地添加依赖属性。 ValidationRule 不从 DependencyObject 扩展
    猜你喜欢
    • 1970-01-01
    • 2011-04-21
    • 2011-07-30
    • 2011-07-22
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多