WPF:以编程方式引发 SelectionChangedEvent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22718717/
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
WPF: Raise programmatically a SelectionChangedEvent
提问by Tom
in WPF, I want raise programmatically a SelectionChanged event on a combobox. I've tried the following code but it doesn't work:
在 WPF 中,我想以编程方式在组合框上引发一个 SelectionChanged 事件。我已经尝试了以下代码,但它不起作用:
myComboBox.RaiseEvent(new RoutedEventArgs(ComboBox.SelectionChangedEvent,
myComboBox));
How can i raise that event?
我怎样才能提出那个事件?
Thanks
谢谢
回答by Meloviz
While the other answers here are good practice, they don't actually answer your question. To actually answer your question of programmatically raising a SelectionChangedEvent, you could do something like the following:
虽然这里的其他答案是很好的做法,但它们实际上并没有回答您的问题。要实际回答以编程方式引发 SelectionChangedEvent 的问题,您可以执行以下操作:
RoutedEvent routed = ComboBox.SelectionChangedEvent;
List<ComboBoxItem> remove = new List<ComboBoxItem> {myComboBox.Items[0] as ComboBoxItem},
add = new List<ComboBoxItem> {myComboBox.SelectedItem as ComboBoxItem};
var e = new SelectionChangedEventArgs(routed, remove, add);
myComboBox.RaiseEvent(e);
Or if you wanted to do it in a single command:
或者,如果您想在单个命令中执行此操作:
myComboBox.RaiseEvent(new SelectionChangedEventArgs(
ComboBox.SelectionChangedEvent,
new List<ComboBoxIem> {myComboBox.Items[0] as ComboBoxItem},
new List<ComboBoxItem> {myComboBox.SelectedItem as ComboBoxItem}));`
Easy as pie. But I agree with @RohitVats and @BradleyDotNet that it would be better to do the same functionality by just creating another method that takes the usual event handler arguments and just call that from any other method.
非常简单。但我同意 @RohitVats 和 @BradleyDotNet 的观点,即通过创建另一种方法来执行相同的功能会更好,该方法采用通常的事件处理程序参数并从任何其他方法调用它。
I'm still going to leave this here in case anyone else doesn't want to take that advice. It's good to know how to raise events this way anyway.
如果其他人不想接受这个建议,我仍然会留在这里。无论如何,知道如何以这种方式引发事件是件好事。
回答by BradleyDotNET
You just said you just need to execute the handler, so you don't need to worry about raising the actual event. Just call the handler (which I'm assuming you have access to since you have access to the combo box itsself):
您刚刚说您只需要执行处理程序,因此您无需担心引发实际事件。只需调用处理程序(我假设您可以访问,因为您可以访问组合框本身):
SelectionChangedHandler(myComboBox, new RoutedEventArgs());
Now thats a bit of a hack, you should really refactor the logic that needs to be rerun into a new function:
现在这有点麻烦,您应该真正将需要重新运行的逻辑重构为新函数:
private void SelectionChangedHandler(object sender, RoutedEventArgs e)
{
...Stuff that you don't rerun
CommonHandleLogic();
}
private void CommonHandleLogic()
{
...Whatever you do
}
Then you can just call CommonHandleLogic()instead of trying to raise the event.
然后您可以直接调用CommonHandleLogic()而不是尝试引发事件。
回答by premes
This came up for me in the context of a ListBox, where the problem could be solved by setting the SelectedItem index. For example :
这对我来说是在 ListBox 的上下文中出现的,可以通过设置 SelectedItem 索引来解决问题。例如 :
myComboBox.SelectedItem = 0;
myComboBox.SelectedItem = 0;
回答by yu yang Jian
By MSDN, A value of negative one (-1) is returned if no item is selected.,
通过MSDN, A value of negative one (-1) is returned if no item is selected.,
So give your combobox a default index other than -1,
所以给你的组合框一个不是 -1 的默认索引,
and switch index to -1 and back to original to invoke the event,
并将索引切换到 -1 并返回到原始以调用事件,
without index really changed:
没有索引真的改变了:
public static void RaiseSelectionChanged(ComboBox cbx)
{
int index = cbx.SelectedIndex;
cbx.SelectedIndex = -1;
cbx.SelectedIndex = index;
}

