wpf 设置 Combobox 的选定值而不触发 SelectionChanged 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21916483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:57:16  来源:igfitidea点击:

Setting a Combobox 's selected value without firing SelectionChanged event

c#wpf

提问by stoic

I have a ComboBox:

我有一个组合框:

<ComboBox Name="drpRoute" SelectionChanged="drpRoute_SelectionChanged" />

And I set the list items in the code behind file:

我在代码隐藏文件中设置了列表项:

public ClientReports()
{
    InitializeComponent();
    drpRoute.AddSelect(...listofcomboxitemshere....)
}


public static class ControlHelpers
{
    public static ComboBox AddSelect(this ComboBox comboBox, IList<ComboBoxItem> source)
    {
        source.Insert(0, new ComboBoxItem { Content = " - select - "});

        comboBox.ItemsSource = source;
        comboBox.SelectedIndex = 0;

        return comboBox;
    }
}

For some reason, when I set the SelectedIndex, the SelectionChangedevent get's fired.

出于某种原因,当我设置 时SelectedIndexSelectionChanged事件被触发。

How on earth do I set the ItemSourceand set the SelectedIndexwithout firing the SelectionChangedevent?

我到底如何设置ItemSource和设置SelectedIndex而不触发SelectionChanged事件?

I am new to WPF, but surely it should not be as complicated as it seems? or am I missing something here?

我是 WPF 的新手,但它肯定不会像看起来那么复杂吗?还是我在这里遗漏了什么?

回答by evanb

The SelectionChangedevent will fire regardless if it was set through code or by user interaction. To get around this you will need to either remove the handler when you are changing it in code as @Viv suggested or add a flag to ignore the changes while you are changing it in code. The first option will not fire the event since you are not listening to it and in the second, you will need to check the flag to see if it was triggered by a change in code.

SelectionChanged无论是通过代码还是通过用户交互设置该事件都会触发。要解决此问题,您需要在按照@Viv 建议在代码中更改处理程序时删除处理程序,或者在代码中更改处理程序时添加一个标志以忽略更改。第一个选项不会触发事件,因为您没有监听它,在第二个选项中,您需要检查标志以查看它是否由代码更改触发。

Update: Here's an example of using a flag:

更新:这是使用标志的示例:

bool codeTriggered = false;

// Where ever you set your selectedindex to 0
codeTriggered = true;
comboBox.SelectedIndex = 0;
codeTriggered = false;

// In your SelectionChanged event handler
if (!codeTriggered)
{
   // Do stuff when user initiated the selection changed
}

回答by Aurelien Souchet

You can solve this problem with data binding :

您可以通过数据绑定解决这个问题:

private int _sourceIndex;
public int SourceIndex
{
    get { return _sourceIndex; }
    set
    {
        _sourceIndex= value;
        NotifyPropertyChanged("SourceIndex");
    }
}

private List<ComboBoxItem> _sourceList;
public List<ComboBoxItem> SourceList
{
    get { return _sourceList; }
    set
    {
        _sourceList= value;
        NotifyPropertyChanged("SourceList");
    }
}

public ClientReports()
{
    InitializeComponent();

    // Set the DataContext
    DataContext = this;

    // set the sourceIndex to 0
    SourceIndex = 0;

    // SourceList initialization
    source = ... // get your comboboxitem list
    source.Insert(0, new ComboBoxItem { Content = " - select - "});
    SourceList = source
}

In the XAML bind SelectedItem and ItemsSource

在 XAML 中绑定 SelectedItem 和 ItemsSource

<ComboBox Name="drpRoute" 
   ItemsSource="{Binding SourceList}"
   SelectedIndex="{Binding SourceIndex}" />

With databinding, everytime you change SourceIndex in your code it changes in the UI, and if you change it in the UI it changes in the class too, you can try to find tutorials about the MVVM design patternit's a great way to write WPF application.

使用数据绑定,每次更改代码中的 SourceIndex 时,它都会在 UI 中发生变化,如果您在 UI 中更改它,它也会在类中发生变化,您可以尝试查找有关MVVM 设计模式的教程,这是编写 WPF 应用程序的好方法.