如何通过WPF组合框中的selectionchanged事件获取所选项目文本? [英] How to get selected item text through selectionchanged event in WPF combobox?

查看:403
本文介绍了如何通过WPF组合框中的selectionchanged事件获取所选项目文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个通用而简单的功能:在一个combox中,通过WPF ComboBox中的用户选择获取所选项目文本,但即使在搜索互联网之后我也找不到办法。难以置信的。希望有人可以帮助我。



XAML和c#代码如下。我已经给出了评论和我的问题以及代码。请注意,我在MainWindow()和Selectionchanged事件中尝试过类似的功能。问题仅发生在后一种功能中。但我需要获取所选项目,例如当我选择combox项目作为第一项时,item1。但是在开始调试时会出现错误(见下文)。



strangly,XAML代码无法完全复制到此处。但是,您应该假设xaml代码没有任何问题。它只是由于这里的编辑器而不完整。



< ComboBox x:Name =   ComBoxTest高度=   50 SelectionChanged =   ComBoxTest_SelectionChanged SelectedValuePath =  内容 >  

< ComboBoxItem IsSelected = True> item1 < / ComboBoxItem >

< ComboBoxItem> item2 < / ComboBoxItem >

< / ComboBox >







 命名空间 WPFComboBoxTest 
{
/// < 摘要 >
/// MainWindow.xaml的交互逻辑
/// < / summary >
< span class =code-keyword> public partial class MainWindow:Window
{
public M. ainWindow()
{
InitializeComponent();

MessageBox.Show(ComBoxTest.SelectedItem.ToString()); // System.Windows .Controls.ComboxItem:item1
MessageBox.Show(ComBoxTest.SelectedValue.ToString()); // item1(OK)
string text =((ComboBoxItem)ComBoxTest.SelectedItem).Content.ToString(); // item1(好的,也是)
MessageBox.Show(text);

}

private void ComBoxTest_SelectionChanged(< span class =code-keyword> object sender,SelectionChangedEventArgs e)
{
MessageBox.Show(ComBoxTest.SelectedItem.ToString()); // System.Windows.Controls.ComboxItem,仅在启动程序后,没有item1,默认选择i XAML代码。但是在启动programm之后,当我选择第一个项目时它会显示System.Windows.Controls.ComboxItem:item1
MessageBox.Show(ComBoxTest.SelectedValue.ToString()); // 问题,开始调试时:发生teyp'System.Refection.TragetInvocationException'的未处理异常。我认为如果我真的点击ComboBox并选择一个项目,这将有效。但是我没有机会因为一旦启动程序就会出现这个错误。
string text =((ComboBoxItem)ComBoxTest.SelectedItem).Content。 ToString(); // 同样的问题
MessageBox.Show(text);
// 我的问题:如何处理这个问题?
}
}
}





我的尝试:



尝试了不同的方式,selectitem,selectedvalue,items,none都无效。

搜索互联网,但不成功。

解决方案

从XAML中删除:

SelectedValuePath =内容



并且异常消失......

I want to realize a general and simple function: in a combox, get the selected item text through user selection in a WPF ComboBox, but I could not find a way to do it even after search internet. Unbelievable. Hope anyone can help me.

the XAML and c# code are as follows. I have given the comments and my question as well the code. Note that I have tried the similar functions in both MainWindow() and Selectionchanged event. The problem occurs only in the latter function. But I need get the selected item, e.g. "item1" when I select the combox item to be the first item. But it gives an error (See below) when starting the debugging.

strangly, the XAML code can not be fully copied to here. however, you should assume the xaml code has no any problem. it is not complete only due to the editor here.

<ComboBox x:Name="ComBoxTest" Height="50" SelectionChanged="ComBoxTest_SelectionChanged" SelectedValuePath="Content">

        <ComboBoxItem IsSelected="True">item1</ComboBoxItem>

        <ComboBoxItem>item2</ComboBoxItem>

</ComboBox>




namespace WPFComboBoxTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        
 MessageBox.Show(ComBoxTest.SelectedItem.ToString());//System.Windows.Controls.ComboxItem:item1
 MessageBox.Show(ComBoxTest.SelectedValue.ToString());//item1 (OK)
string text = ((ComboBoxItem)ComBoxTest.SelectedItem).Content.ToString();//item1 (Ok, too)
            MessageBox.Show(text);
           
        }

        private void ComBoxTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageBox.Show(ComBoxTest.SelectedItem.ToString());//System.Windows.Controls.ComboxItem, only, after starting the programm, no item1, which is selected by default i XAML code. But after starting programm, it shows System.Windows.Controls.ComboxItem:item1 when I select the first item
            MessageBox.Show(ComBoxTest.SelectedValue.ToString());//problem, when start debugging:  an unhandled exception of teyp 'System.Refection.TragetInvocationException' occured. I think this will work if I really hit the ComboBox and select one item. But I have no chance since this error occurs once starting the programm.
            string text = ((ComboBoxItem)ComBoxTest.SelectedItem).Content.ToString();//same problem
            MessageBox.Show(text);
            //my question: how to deal with this problem?
        }
    }
}



What I have tried:

tried different ways, selecteditem, selectedvalue, items, none works.
search also internet, but it is not successful.

解决方案

Remove this from XAML :
SelectedValuePath="Content"

and exception is gone...


这篇关于如何通过WPF组合框中的selectionchanged事件获取所选项目文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆