问题
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 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);
}
}
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.
回答1:
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.
来源:https://stackoverflow.com/questions/26596373/validation-rule-for-radio-buttons-wpf