C#UWP如何获取更改后的ComboBoxItem的值 [英] C# UWP how to get the value of changed ComboBoxItem

查看:95
本文介绍了C#UWP如何获取更改后的ComboBoxItem的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.xaml文件中,我的组合框如下:

In my .xaml file I have my combo box as below:

< ComboBox Name = CLengthCombo SelectionChanged = ComboBox_SelectionChanged >
< ComboBoxItem Content = 24 />
< ComboBoxItem Content = 25 />
< ComboBoxItem Content = 26 IsSelected = True />
< ComboBoxItem Content = 27 />
< / ComboBox>

如何实现我的ComboBox_SelectionChanged事件,以便获得应用程序运行时用户更改的comboBoxItem的内容?在这种情况下,即使使用SelectionChanged事件也是正确的吗?以下内容不起作用:

<ComboBox Name="CLengthCombo" SelectionChanged="ComboBox_SelectionChanged"> <ComboBoxItem Content="24"/> <ComboBoxItem Content="25"/> <ComboBoxItem Content="26" IsSelected="True"/> <ComboBoxItem Content="27"/> </ComboBox> how can I implement my ComboBox_SelectionChanged event so that I can get the content of the comboBoxItem which is changed by user when application is running? Is SelectionChanged event the correct even to use in this case? The below does not work:

private void ComboBox_SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{字符串selectedItem = CLengthCombo.PlaceholderText; }

预先感谢您的帮助!

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string chosenItem = CLengthCombo.PlaceholderText; } Thanks in advance for your help!

推荐答案

您可以按照以下步骤操作

You can do it like following

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var comboBoxItem = e.AddedItems[0] as ComboBoxItem;
            if (comboBoxItem == null) return;
            var content = comboBoxItem.Content as string;
            if (content != null && content.Equals("some text"))
            {
                //do what ever you want
            }
        }

这篇关于C#UWP如何获取更改后的ComboBoxItem的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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