WPF组合框验证

WPF组合框验证,wpf,validation,combobox,Wpf,Validation,Combobox,我有一个带有性别的组合框(男,女..):我要求用户选择一个值(组合框默认没有值) 问题是它从未输入此[string columnname] 当我尝试一个名为的文本框时,它会输入[string columnname],一切正常: <TextBox Style="{StaticResource textBoxInError}" Text="{Binding Path=Surname, ValidatesOnDataErrors=True, UpdateSourceTrigger=Prop

我有一个带有性别的组合框(男,女..):我要求用户选择一个值(组合框默认没有值)

问题是它从未输入此[string columnname]

当我尝试一个名为的文本框时,它会输入[string columnname],一切正常:

<TextBox  Style="{StaticResource textBoxInError}"   Text="{Binding Path=Surname, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"/>

我决定这样做:

当用户单击“保存”时,将进行验证。 然后,如果一个SelectedValue为null,我只需签入一个验证事件。 如果是这样,那么这意味着用户没有选择任何项目。 然后我警告他这个事实

private void CanSave(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = myComboBox.SelectedValue != null;
}

windows中的好方法是在组合框中添加一个值(None),并测试此人是否包含该值(None)。“(None)”是元选项,因为它不是选项的有效值,而是描述选项本身未被使用

正确:
(来源:)

不正确:
(来源:)

验证在您的案例中不起作用,因为当您想说没有选择性别时,没有选择任何值

<TextBox  Style="{StaticResource textBoxInError}"   Text="{Binding Path=Surname, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"/>
private void CanSave(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = myComboBox.SelectedValue != null;
}