简体   繁体   English

wpf单选按钮的验证规则

[英]Validation Rule for Radio buttons wpf

I have a RadioButton inside ItemsControl . 我在ItemsControl有一个RadioButton By default Radio Buttons will be unchecked. 默认情况下,单选按钮将处于未选中状态。 I want the user to select either of the radio buttons if a particular value (string value) is configured in another screen. 如果在另一个屏幕中配置了特定值(字符串值),我希望用户选择其中一个单选按钮。 I need to apply a validation rule for the same. 我需要为它应用验证规则。 If the user does not select either of the radio buttons, then on click on a submit button, I should display validation error. 如果用户选择任何一个单选按钮,则在单击提交按钮时,我应该显示验证错误。

XAML XAML

<StackPanel Grid.Row="1" Visibility="{Binding Path=IsUpdateSendDateConfigured}" Orientation="Horizontal" Margin="0,2,0,0">
        <TextBlock Text="{Binding Path=UpdateSendDate, UpdateSourceTrigger=PropertyChanged}" FontWeight="Bold" FontSize="12" Width="400" TextWrapping="WrapWithOverflow" />
        <ItemsControl Name="updateSendDateLevelItemsControl" ItemsSource="{Binding UpdateSendDateLevel}" Margin="10,5,0,10">
                 <ItemsControl.ItemsPanel>
                         <ItemsPanelTemplate>
                                <StackPanel Orientation="Horizontal"/>
                         </ItemsPanelTemplate>
                 </ItemsControl.ItemsPanel>
                 <ItemsControl.ItemTemplate>
                         <DataTemplate>
                                <RadioButton Height="25" GroupName="updateSendDateLevel" IsChecked="{Binding Selected, ValidatesOnDataErrors=True}" Padding="10,0,10,0" >  <!--Need to apply Validation rule here -->
                                       <TextBlock Text="{Binding Description}"/>
                                </RadioButton>
                         </DataTemplate>
                  </ItemsControl.ItemTemplate>
        </ItemsControl>
</StackPanel>

UpdateSendDateLevel is a view model which I am updating in the controller. UpdateSendDateLevel是我正在控制器中更新的视图模型。

if (!string.IsNullOrEmpty(configuredUpdateDueDateSelection))
{
       UpdateSendDateViewModel updateSendDateLevelViewModel = null;
       foreach (SendDate value in Enum.GetValues(typeof(SendDate)).Cast<SendDate>())
       {
             updateSendDateLevelViewModel = new UpdateSendDateViewModel();
             updateSendDateLevelViewModel.UpdateSendDateLevel = value;
             updateSendDateLevelViewModel.Description = EnumHelper.GetDescription(value);

             m_sendDataContext.UpdateSendDateLevel.Add(updateSendDateLevelViewModel);
        }

} 

Can someone please help with with adding xaml side validation rule or point me in the right direction? 有人可以帮忙添加xaml侧面验证规则,还是向我指出正确的方向?

Let me know if you need any other details. 让我知道您是否需要其他详细信息。

ValidationRules can be added as an extension of the Binding property, like so: 可以将ValidationRules添加为Binding属性的扩展,如下所示:

<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.) (上面的说明,如果您要做的只是将文本放入块中,则实际上不需要使用TextBlock。您可以在RadioButton的Content =“”字段中包括该文本绑定。)

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. 然后,您还需要定义一个从ValidationRule继承并覆盖公共ValidationResult Validate(对象值,CultureInfo cultureInfo)的对象(YourValidationRule),并将静态引用(在这种情况下为规则)添加到自定义ValidationRule所在的名称空间存在。

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 MSDN上存在有关ValidationRules的深入教程, 网址为: 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. 但是,Miiko是正确的-您可能更容易使用实现ICommand的对象,并使用CanExecute确定客户是否可以继续。 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. 这样做的主要缺点是,虚幻的,无法使用的按钮不一定具有通信性,因此您应小心确保客户了解为什么他们无法使用该按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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