WPF 防止组合框选择在条件下发生变化
WPF prevent combobox selection change under condition
我有一个 ComboBox,其选择的索引被绑定为一个整数。我想做的是在不满足特定条件的情况下防止组合框选择发生变化。这是我目前所拥有的
在xaml中:
<ComboBox SelectedIndex="{Binding CurrentMinCondition,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<ComboBoxItem>NA</ComboBoxItem>
<ComboBoxItem>LT</ComboBoxItem>
<ComboBoxItem>GT</ComboBoxItem>
</ComboBox>
在 CS:
private int _currentMinCondition = 0;
public int CurrentMinCondition
{
get { return _currentMinCondition; }
set
{
if (/*Condition is met*/){
_currentMinCondition = value;
OnPropertyChanged("CurrentMinCondition");
}
else
{
MessageBox.Show("Error");
_currentMinCondition = 0;
OnPropertyChanged("CurrentMinCondition");
}
}
}
现在我认为这行得通,但发生的事情是,当我从组合框中更改我的选择并且不满足条件时,我的 MessageBox
显示错误但图形组合框更改了它的选择。我怎样才能防止这种情况发生?
您可以使用错误验证。如果结果不是预期的,您可以 return 错误的验证结果:
<ComboBox SelectedIndex="{Binding CurrentMinCondition,Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
ValidateOnDataErrors=True,
NotifyOnValidationError=True,
ValidatesOnExceptions=True}",
Validation.Error="MethodToValidate">
<ComboBoxItem>NA</ComboBoxItem>
<ComboBoxItem>LT</ComboBoxItem>
<ComboBoxItem>GT</ComboBoxItem>
</ComboBox>
在属性中你应该抛出异常:
public int CurrentMinCondition
{
get {return _currentMinCondition;}
set
{
if(value != _currentMinCondition)
{
if(condition met)
throw new Exception("Message error");
else
{
_currentMinCondition = value;
PropertyChanged("CurrentMinCondition");
}
}
}
}
然后在你的验证方法中:
private void ValidateMethod(object sender, ValidationErrorEventArgs e)
{
e.Handled = true;
}
这将用错误矩形标记组合框,如果有错误,基础值将不会更改。
我有一个 ComboBox,其选择的索引被绑定为一个整数。我想做的是在不满足特定条件的情况下防止组合框选择发生变化。这是我目前所拥有的
在xaml中:
<ComboBox SelectedIndex="{Binding CurrentMinCondition,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
<ComboBoxItem>NA</ComboBoxItem>
<ComboBoxItem>LT</ComboBoxItem>
<ComboBoxItem>GT</ComboBoxItem>
</ComboBox>
在 CS:
private int _currentMinCondition = 0;
public int CurrentMinCondition
{
get { return _currentMinCondition; }
set
{
if (/*Condition is met*/){
_currentMinCondition = value;
OnPropertyChanged("CurrentMinCondition");
}
else
{
MessageBox.Show("Error");
_currentMinCondition = 0;
OnPropertyChanged("CurrentMinCondition");
}
}
}
现在我认为这行得通,但发生的事情是,当我从组合框中更改我的选择并且不满足条件时,我的 MessageBox
显示错误但图形组合框更改了它的选择。我怎样才能防止这种情况发生?
您可以使用错误验证。如果结果不是预期的,您可以 return 错误的验证结果:
<ComboBox SelectedIndex="{Binding CurrentMinCondition,Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
ValidateOnDataErrors=True,
NotifyOnValidationError=True,
ValidatesOnExceptions=True}",
Validation.Error="MethodToValidate">
<ComboBoxItem>NA</ComboBoxItem>
<ComboBoxItem>LT</ComboBoxItem>
<ComboBoxItem>GT</ComboBoxItem>
</ComboBox>
在属性中你应该抛出异常:
public int CurrentMinCondition
{
get {return _currentMinCondition;}
set
{
if(value != _currentMinCondition)
{
if(condition met)
throw new Exception("Message error");
else
{
_currentMinCondition = value;
PropertyChanged("CurrentMinCondition");
}
}
}
}
然后在你的验证方法中:
private void ValidateMethod(object sender, ValidationErrorEventArgs e)
{
e.Handled = true;
}
这将用错误矩形标记组合框,如果有错误,基础值将不会更改。