-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