2009-07-28 52 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; 
}