Validation Rule for Radio buttons wpf

前端 未结 1 2007
无人及你
无人及你 2021-01-25 17:23

I have a RadioButton inside ItemsControl. By default Radio Buttons will be unchecked. I want the user to select either of the radio buttons if a partic

相关标签:
1条回答
  • 2021-01-25 18:16

    ValidationRules can be added as an extension of the Binding property, like so:

    <RadioButton>
        <TextBlock Text="{Binding Description}"/>
        <RadioButton.IsChecked>
            <Binding Path="Selected" UpdateSourceTrigger="PropertyChanged">
              <Binding.ValidationRules>
                <rules:YourValidationRule Min="21" Max="130"/>
              </Binding.ValidationRules>
            </Binding>
        </RadioButton.IsChecked>
    </RadioButton>
    

    (Note for the above that you don't actually need to use a TextBlock if all you're doing is putting text in the block. You can just include that text binding in the Content="" field on the RadioButton.)

    Then you'd also need to define an object (YourValidationRule) that inherits from ValidationRule and overrides public ValidationResult Validate(object value, CultureInfo cultureInfo), and add a static reference (rules, in this case) to the namespace in which your custom ValidationRule exists.

    An in-depth tutorial for ValidationRules exists on MSDN at this link: http://msdn.microsoft.com/en-us/library/ms753962%28v=vs.110%29.aspx

    However, Miiko is correct - it may be easier for you to use an object that implements ICommand, and use CanExecute to determine whether the customer may proceed. The main downside of this is that a ghosted, unusable button is not necessarily communicative, and you should be careful to ensure that your customers understand why they're unable to use the button.

    0 讨论(0)
提交回复
热议问题