[英]Cancel Combobox Selection Changed event using behaviour
I have a combobox which will load data in the data grid based on selection change. 我有一个组合框,它将根据选择更改将数据加载到数据网格中。 On change of combobox selection i need to check if the current data in data grid is correct or not .
在更改组合框选择时,我需要检查数据网格中的当前数据是否正确。 if not correct i would like to cancel the combobox selection change.
如果不正确,我想取消组合框选择更改。
here is my behavior class 这是我的行为课
public class ComboBoxSelectionBehaviour : Behavior<ComboBox>
{
public static readonly DependencyProperty SourceProperty = DependencyProperty.RegisterAttached(
"Source",
typeof(ViewModel),
typeof(ComboBoxSelectionBehaviour),
new PropertyMetadata(null));
public ViewModel Source
{
get { return (ViewModel)GetValue(SourceProperty); }
set { SetValue(SourceProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged; ;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
}
private void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var combo = sender as ComboBox;
if (Source != null)
{
// Suppress the event if errors exist
if (!Source.IsDataCorrect())
{
e.Handled = true;
}
}
}
}
even after handling the event combobox selected item is getting changed. 即使处理了事件组合框之后,所选项目也已更改。
Please give some suggestions to solve this issue. 请提供一些解决此问题的建议。
Could you simply just do a 你能简单地做一个
myComboBox.SelectedIndex--
If the data is not correct? 如果数据不正确? Or would this cause an infinite loop?
还是会导致无限循环?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.