简体   繁体   English

如何测试最近更改了哪个组合框? WPF

[英]How to test which combobox has been most recently changed? WPF

Is there a way to check to see which combobox which value has been most recently changed? 有没有一种方法可以查看哪个组合框哪个值最近被更改了?

how to check if item is selected from a comboBox in C# 如何检查是否从C#中的comboBox中选择了项目

I know you can do this, 我知道你可以做到

if (MyComboBox.SelectedIndex >= 0)
{
    //do stuff
}

The problem I am having is that I am combining Event Handlers in to one handler due to the amount of comboboxes and having one event for each combobox really is not practical if I can help it. 我遇到的问题是由于组合框的数量,我将事件处理程序组合到一个处理程序中,如果我能帮上忙,那么每个组合框都只有一个事件是不切实际的。

Is there a way to have a variable assigned giving you the name of the combobox which value has been most recently changed? 有没有一种方法可以给变量赋一个组合框名称,该值最近被更改了? Or will I have to use individual event handlers for each combobox? 还是我必须为每个组合框使用单独的事件处理程序?

Actually It will be very easy to track most recent combobox change when you using single event handler for all combobox. 实际上,当对所有组合框使用单个事件处理程序时,跟踪最近的组合框更改将非常容易。 You can do by following way. 您可以按照以下方式进行。

string lastComboName=""; // define global variable  
//common event handler for all combobox 
 private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
    var cmb = (ComboBox)sender; 
    lastComboName = cmb.Name;
 }

Hope that each comboBox having an unique name, then we can use those name to identify which one is the sender of event: Now consider the following code for this: 希望每个comboBox都有一个唯一的名称,然后我们可以使用这些名称来标识哪个是事件的发送者:现在考虑以下代码:

private void CboFirst_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox selctedComboBox = sender as ComboBox;
    string ComboName = selctedComboBox.Name;
    // Do something
}

Now tracking the last updated ComboBox, You can achieve this by keeping a blobal variable and update it in every trigger so Every time it holds the Latest value( the name of the combobox) 现在跟踪最近更新的ComboBox,您可以通过保留一个blobal变量并在每个触发器中对其进行更新来实现,以便每次它包含最新值(组合框的名称)时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM