问题描述
我在ItemsControl
中有一个RadioButton
.默认情况下,单选按钮将处于未选中状态.如果在另一个屏幕中配置了特定值(字符串值),我希望用户选择其中一个单选按钮.我需要为它应用验证规则.如果用户没有选择任何一个单选按钮,则在单击提交按钮时,我应该显示验证错误.
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 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
<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是我正在控制器中更新的视图模型.
UpdateSendDateLevel is a view model which I am updating in the controller.
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);
}
}
有人可以帮忙添加xaml侧面验证规则,还是向我指出正确的方向?
Can someone please help with with adding xaml side validation rule or point me in the right direction?
让我知道是否需要其他详细信息.
Let me know if you need any other details.
推荐答案
ValidationRules可以作为Binding属性的扩展添加,如下所示:
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>
(以上所述,如果您要做的只是将文本放入块中,则实际上不需要使用TextBlock.您可以仅在RadioButton的Content ="字段中包含该文本绑定. )
(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.)
然后,您还需要定义一个从ValidationRule继承并覆盖公共ValidationResult Validate(对象值,CultureInfo cultureInfo)的对象(YourValidationRule),并向其中命名空间添加静态引用(在这种情况下为规则)您的自定义ValidationRule存在.
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.
MSDN上的以下链接提供了ValidationRules的深入教程: http://msdn.microsoft.com/en-us/library/ms753962%28v=vs.110%29.aspx
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
但是,Miiko是正确的-您可能更容易使用实现ICommand的对象,并使用CanExecute确定客户是否可以继续.这样做的主要缺点是,鬼影的,无法使用的按钮不一定能传达信息,因此您应注意确保客户了解为什么他们无法使用该按钮.
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.
这篇关于wpf单选按钮的验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!