2015-09-20 61 views
-2

我想對組合框選定值進行數據驗證。它的默認值是0,所以我想它應該選擇與任何值不爲0.WPF:如何通過依賴屬性在combobox selectedvalue中進行數據驗證

我已綁定它的視圖模型中的int依賴項屬性選定的值。 我知道我們可以通過依賴項屬性進行數據驗證,所以任何幫助我們都非常感謝。

<ComboBox x:Name="cmbBox" Width="100" Margin="10,0,20,0" ItemsSource="{Binding Path=TotalPeople}" 
       SelectedItem="{Binding Mode=TwoWay, Path=SelectedPeopleNumber}" Height="20"/> 

這裏是viewmodel的依賴屬性代碼。

public ExpenseViewModel() 
{ 
    totalPeople = new List<int>(); 
    populate(); 
} 

private void populate() 
{ 
    totalPeople.Add(2); 
    totalPeople.Add(3); 
    totalPeople.Add(4); 
    totalPeople.Add(5); 
} 
private List<int> totalPeople; 
public List<int> TotalPeople 
{ 
     get { return totalPeople; } 
     set 
     { 
      if (totalPeople != value) 
      { 
       totalPeople = value; 
       NotifyPropertyChanged("TotalPeople"); 
      } 
     } 
} 
public int SelectedPeopleNumber 
{ 
    get { return (int)GetValue(SelectedPeopleNumberProperty); } 
    set { SetValue(SelectedPeopleNumberProperty, value); } 
} 

// Using a DependencyProperty as the backing store for SelectedPeopleNumber. This enables animation, styling, binding, etc... 
public static readonly DependencyProperty SelectedPeopleNumberProperty = 
     DependencyProperty.Register("SelectedPeopleNumber", typeof(int), typeof(ExpenseViewModel), new PropertyMetadata(0)); 

我想組合框應該是紅色邊框時默認值爲0

回答

2

你似乎DependencyProperty和數據綁定Property之間已經弄糊塗了。

數據綁定屬性是具有用於在控件中顯示數據以及存儲選定值的源項目的屬性。

DependencyProperty(DP)是您綁定上述數據綁定屬性的依賴屬性。

在你的Xaml代碼SelectedItem是一個DP,你已經將它綁定到另一個DP(SelectedPeopleNumber),我猜你不打算這麼做。

下面是固定的代碼

<ComboBox x:Name="cmbBox" Width="100" Margin="10,0,20,0" ItemsSource="{Binding Path=TotalPeople}" 
      SelectedItem="{Binding Path=SelectedIndex, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" Height="20"> 
    <ComboBox.Resources> 
     <Style TargetType="{x:Type ComboBox}"> 
      <Style.Triggers> 
       <Trigger Property="Validation.HasError" Value="True"> 
        <Setter Property="ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, 
            RelativeSource={x:Static RelativeSource.Self}}"/> 
       </Trigger> 
      </Style.Triggers> 

     </Style> 
    </ComboBox.Resources> 
</ComboBox> 

視圖模型

class ExpenseViewModel : INotifyPropertyChanged, IDataErrorInfo 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    public void NotifyPropertyChanged(string propName) 
    { 
     var pc = PropertyChanged; 
     if (pc != null) 
     { 
      pc(this, new PropertyChangedEventArgs(propName)); 
     } 
    } 

    private void populate() 
    { 
     _totalPeople.Add(2); 
     _totalPeople.Add(3); 
     _totalPeople.Add(4); 
     _totalPeople.Add(5); 
     NotifyPropertyChanged("TotalPeople"); 
    } 
    private List<int> _totalPeople; 
    public IEnumerable<int> TotalPeople 
    { 
     get { return _totalPeople; } 
    } 

    public ExpenseViewModel() 
    { 
     _totalPeople = new List<int>(); 
     populate(); 
    } 

    public int SelectedIndex { get; set; } 

    private string _error; 
    public string Error 
    { 
     get { return _error; } 
    } 

    public string this[string columnName] 
    { 
     get 
     { 
      if (columnName.Equals("SelectedIndex")) 
      { 
       if(SelectedIndex==0) 
       { 
        _error = "Please Select One"; 
       } 
      } 
      return Error; 
     } 
    } 
}