Silverlight - 如何在组合框中获取所选项目的文本 [英] Silverlight - how do I get the text of the selected item in a combobox

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

问题描述

简单的给大家...

我是 Silverlight 的新手,确实缺少数据表之类的东西.我目前还在苦苦挣扎的是如何获取我的组合框当前所选项目的文本.在 winforms 中我会这样做:

I'm new to Silverlight and really missing stuff like DataTables and things. What I'm also currently struggling with is how to get the text of my combobox's currently selected item. In winforms I would have done:

ComboBox myCombo = new ComboBox.......
string selected = myCombo.Text;

我正在努力如何获取这些信息.

I'm struggling how to get this info out.

推荐答案

组合框中的选定项目是当前持有的任何类型的项目.因此,如果您将绑定设置为字符串集合,则所选项目将是一个字符串:

The selected item of your combo box is whatever type of item is currently holding. So if you set the binding to a collection of strings, then the selected item will be a string:

string mySelectedValue = ((string)MyComboBox.SelectedItem);

如果它是一个更复杂的对象,您将需要转换并使用预期的对象.如果您有使用列表框项类的 XAML,例如:

If it is a more complex object you will need to cast and use the expected object. If you have XAML using the list box item class, like:

<ComboBox x:Name="MyComboBox">
    <ComboBox.Items>
        <ComboBoxItem>
            <TextBlock Text="Hello World"/>
        </ComboBoxItem>
    </ComboBox.Items>
</ComboBox>

然后您将像这样访问所选项目:

Then you would access the selected item like this:

string mySelectedValue = 
  ((TextBlock)((ComboBoxItem)MyComboBox.SelectedItem).Content).Text;

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

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