简体   繁体   中英

wpf binding property in ValidationRule

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 ?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM