2009-07-28 140 views
4

我发生性关系组合框(男,女..):我从用户的需求来选择一个值(组合框没有默认值)。WPF组合框验证

<ComboBox ItemsSource="{x:Static Member=data:Sex.AllTypes}" SelectedItem="{Binding Path=Sex.Value, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" VerticalAlignment="Top"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <TextBlock Text="{Binding Path=Name}" /> 
        </StackPanel> 
      </DataTemplate> 
     </ComboBox.ItemTemplate> 
</ComboBox> 

Sex.Value是在我的个人类物业:

public class Person : IDataErrorInfo 
{ 
public string this[string columnName] 
     { 
      get 
      {     
       switch (columnName) 
       {     
        case "Sex": return Sex.Value == null ? "Required field" : null; 
        case "Surname": return string.IsNullOrEmpty(Nachname) ? "Required field" : null; 
       }     
      } 
     } 
public string Error 
     { 
      get 
      { 
       return null; 
      } 
     } 
} 

的问题是,它不会进入此[字符串列名。

当我尝试用名称文本框,它进入这个[字符串列名],一切工作正常:

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

回答

0

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

正确: alt text http://i.msdn.microsoft.com/Aa511458.DropDownLists41(en-us,MSDN.10).png

错误: alt text http://i.msdn.microsoft.com/Aa511458.DropDownLists40(en-us,MSDN.10).png

验证,因为当你想说的是,没有性选择没有选择值在你的情况不工作...

0

我决定做这种方式:

当用户点击保存,验证发生。 然后我只需签入验证事件,如果SelectedValue为空。 如果是这种情况,那么这意味着用户没有选择任何项目。 然后我警告他这个事实。

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