Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置组合框';s所选值而不触发SelectionChanged事件_C#_Wpf - Fatal编程技术网

C# 设置组合框';s所选值而不触发SelectionChanged事件

C# 设置组合框';s所选值而不触发SelectionChanged事件,c#,wpf,C#,Wpf,我有一个组合框: <ComboBox Name="drpRoute" SelectionChanged="drpRoute_SelectionChanged" /> 我在代码隐藏文件中设置了列表项: public ClientReports() { InitializeComponent(); drpRoute.AddSelect(...listofcomboxitemshere....) } public static class ControlHelpe

我有一个组合框:

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

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

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;
    }
}
publicclientreports()
{
初始化组件();
drpRoute.AddSelect(…列表comboxitemshere…)
}
公共静态类控件助手
{
公共静态组合框AddSelect(此组合框组合框,IList源)
{
插入(0,新ComboBoxItem{Content=“-select-”});
comboBox.ItemsSource=源;
comboBox.SelectedIndex=0;
返回组合框;
}
}
出于某种原因,当我设置
SelectedIndex
时,
SelectionChanged
事件get被触发

在不触发
SelectionChanged
事件的情况下,我究竟如何设置
ItemSource
SelectedIndex


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

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

更新: 下面是使用标志的示例:

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
}

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

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
}
private int\u sourceIndex;
公共int源索引
{
获取{return\u sourceIndex;}
设置
{
_sourceIndex=value;
NotifyPropertyChanged(“SourceIndex”);
}
}
私有列表_sourceList;
公共列表源列表
{
获取{return\u sourceList;}
设置
{
_sourceList=值;
NotifyPropertyChanged(“源列表”);
}
}
公共客户端端口()
{
初始化组件();
//设置数据上下文
DataContext=this;
//将sourceIndex设置为0
SourceIndex=0;
//源列表初始化
source=…//获取您的comboboxitem列表
插入(0,新ComboBoxItem{Content=“-select-”});
SourceList=source
}
在XAML绑定中,选择EdItem和ItemsSource

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


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

为什么不希望它触发
SelectionChanged
事件?我不确定是否正确地理解了您的问题,但在选择索引更改时触发“Selection Changed”对我来说似乎是合乎逻辑的。如果您不希望在索引设置为0时调用它,请在
SelectionChanged
处理程序中检查新索引,如果它为0,只需退出,而不执行其余的选择更改逻辑。我获取数据源,我使用默认的“select”设置下拉列表。在这之后,用户可以选择一个项目,当他们在那里选择它时,我
SelectionChanged
event get's firefed and我会根据下拉选择在页面上列出一大堆东西。现在发生的情况是,当我设置原始数据源时,由于加载时触发了
SelectionChanged
事件,因此执行选择逻辑。或者在将SelectedIndex设置为0之前,请在设置SelectedIndex后移除处理程序并再次附加它。类似于
comboBox.SelectionChanged-=drpRoute\u SelectionChanged;comboBox.SelectedIndex=0;comboBox.SelectionChanged+=drpRoute\u SelectionChanged因此您希望水印显示“选择”。你能把
组合框的
Text
属性设置为“Select”吗?这是解决问题的正确方法,因为不可能像其他答案注释那样删除SelectionChanged事件。MVVM不是在这个项目中实现的,它是一个小的单页应用程序:(我写的示例不是真正的MVVM(mvvm将有一个ModelView作为datacontext)但只需一些简单的数据绑定,您就可以使用它来解决这个特定问题,并在工作正常时使用事件。@LordTakkera我不建议删除
SelectionChanged
事件,而是删除事件处理程序。事实上,您可以删除事件处理程序。@Evab我完全同意。感谢您澄清“添加一个标志以忽略更改”当您在代码中更改它时,“我在寻找什么,但不确定如何完成。