Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 单选按钮wpf的验证规则_C#_Wpf_Binding_Radio Button_Wpf Controls - Fatal编程技术网

C# 单选按钮wpf的验证规则

C# 单选按钮wpf的验证规则,c#,wpf,binding,radio-button,wpf-controls,C#,Wpf,Binding,Radio Button,Wpf Controls,我在ItemsControl内有一个单选按钮。默认情况下,单选按钮将被取消选中。如果在另一个屏幕中配置了特定值(字符串值),我希望用户选择其中一个单选按钮。我需要为相同的应用验证规则。如果用户没有选择任何一个单选按钮,则单击提交按钮,我将显示验证错误 XAML <StackPanel Grid.Row="1" Visibility="{Binding Path=IsUpdateSendDateConfigured}" Orientation="Horizontal" Margin="0,2

我在
ItemsControl
内有一个
单选按钮。默认情况下,单选按钮将被取消选中。如果在另一个屏幕中配置了特定值(字符串值),我希望用户选择其中一个单选按钮。我需要为相同的应用验证规则。如果用户没有选择任何一个单选按钮,则单击提交按钮,我将显示验证错误

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是我正在控制器中更新的视图模型

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

} 
if(!string.IsNullOrEmpty(configuredUpdateDueDateSelection))
{
UpdateSendDateViewModel updateSendDateLevelViewModel=null;
foreach(Enum.GetValues(typeof(SendDate)).Cast()中的SendDate值)
{
updateSendDateLevelViewModel=新的UpdateSendDateViewModel();
updateSendDateLevelViewModel.UpdateSendDateLevel=值;
updateSendDateLevelViewModel.Description=EnumHelper.GetDescription(值);
m_sendDataContext.UpdateSendDateLevel.Add(updateSendDateLevelViewModel);
}
} 
是否有人可以帮助添加xaml端验证规则,或者为我指出正确的方向


如果您需要任何其他详细信息,请告诉我。

可以将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>

(请注意,如果您所做的只是将文本放入块中,则实际上不需要使用TextBlock。您只需将该文本绑定包含在RadioButton的Content=”“字段中即可。)

然后还需要定义一个对象(YourValidationRule),该对象继承自ValidationRule并覆盖公共ValidationResult Validate(对象值,CultureInfo CultureInfo),并向自定义ValidationRule所在的命名空间添加一个静态引用(本例中为规则)

MSDN上有一个关于ValidationRules的深入教程,链接如下:


但是,Miiko是正确的-使用实现ICommand的对象,并使用CanExecute来确定客户是否可以继续,可能会更容易。这样做的主要缺点是,一个伪装的、不可用的按钮不一定是交流的,您应该小心确保您的客户理解他们无法使用按钮的原因。

为什么不将按钮绑定到ICommand,让CanExecute处理提交按钮的启用/禁用状态,而不是在之后显示错误?按钮还有许多其他字段需要验证。正确。您可以使用验证规则在控件后显示红色感叹号或其他任何内容,以可视化这是与ICommand实现结合使用的强制输入。从用户体验的角度来看,你可以问哪一个更好a)允许用户在任何时候单击按钮并显示可关闭的错误消息,或者提供视觉提示并仅在所有选择都符合要求时启用按钮。如何为单选按钮添加绑定路径?我做过文本框的扩展,但从来没有做过单选按钮的扩展。@MikkoViitala是的,我不是故意让它看起来好像我觉得一种方式就是另一种方式。我个人的偏好是对话框,它确切地告诉用户他们忽略了做什么,但这是MVVM的设计模式。@Vinay,这完全取决于您试图绑定的内容。我将充实上面的IsChecked属性示例。谢谢@furkle,如果你能给我一个单选按钮IsChecked的示例,那将非常有帮助。