c# WPF如何从方法中调用组合框

本文关键字:调用 组合 方法 WPF | 更新日期: 2023-09-27 18:05:45

我需要从一个方法中调用一个组合框SelectionChanged

在Google上只有4个搜索结果,没有工作。

组合框

private void ComboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //actions
}

在WinForms中我使用了这个,效果很好:

MyMethod(){    
    //call combobox
    ComboBox1_SelectionChanged(sender, e);
}

在WPF中我尝试:

MyMethod(){   
    //call combobox
    ComboBox1.RaiseEvent(new RoutedEventArgs(ComboBox.SelectionChangedEvent));
}

但它不工作。

异常:抛出:"Object of type 'System.Windows. "RoutedEventArgs'不能转换为'System.Windows.Controls.SelectionChangedEventArgs'类型。"

c# WPF如何从方法中调用组合框

我认为WPF与winform是相同的,你也可以使用winform的代码来调用"SelectionChanged"处理程序方法,就像我下面的代码:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Debug.WriteLine("called");
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.ComboBox_SelectionChanged(sender,null);
    }