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

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

问题描述

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

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

<ComboBoxItem Content="24"/><ComboBoxItem Content="25"/><ComboBoxItem Content="26" IsSelected="True"/><ComboBoxItem Content="27"/></组合框>如何实现我的 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(object sender, SelectionChangedEventArgs e){ string selectedItem = CLengthCombo.PlaceholderText;}在此先感谢您的帮助!

推荐答案

你可以像下面这样操作

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天全站免登陆