wpf 获取仅包含文本条目的组合框的选定文本的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3721430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the simplest way to get the selected text of a combo box containing only text entries?
提问by DeveloperDan
My WPF ComboBox contains only text entries. The user will select one. What is the simplest way to get the text of the selected ComboBoxItem?Please answer in both C# and Visual Basic. Here is my ComboBox:
我的 WPF ComboBox 仅包含文本条目。用户将选择一个。获取所选 ComboBoxItem 文本的最简单方法是什么?请用 C# 和 Visual Basic 回答。这是我的组合框:
<ComboBox Name="cboPickOne">
<ComboBoxItem>This</ComboBoxItem>
<ComboBoxItem>should be</ComboBoxItem>
<ComboBoxItem>easier!</ComboBoxItem>
</ComboBox>
By the way, I know the answer but it wasn't easy to find. I thought I'd post the question to help others. REVISION: I've learned a better answer. By adding SelectedValuePath="Content" as a ComboBox attribute I no longer need the ugly casting code. See Andy's answer below.
顺便说一句,我知道答案,但并不容易找到。我想我会发布这个问题来帮助其他人。修订:我学到了一个更好的答案。通过添加 SelectedValuePath="Content" 作为 ComboBox 属性,我不再需要丑陋的转换代码。请参阅下面安迪的回答。
回答by Andy
<ComboBox
Name="cboPickOne"
SelectedValuePath="Content"
>
<ComboBoxItem>This</ComboBoxItem>
<ComboBoxItem>should be</ComboBoxItem>
<ComboBoxItem>easier!</ComboBoxItem>
</ComboBox>
In code:
在代码中:
stringValue = cboPickOne.SelectedValue.ToString()
回答by DeveloperDan
Just to clarify Heinzi and Jim Brissom's answers here is the code in Visual Basic:
为了澄清 Heinzi 和 Jim Brissom 的答案,这里是Visual Basic 中的代码 :
Dim text As String = DirectCast(cboPickOne.SelectedItem, ComboBoxItem).Content.ToString()
and C#:
和C#:
string text = ((ComboBoxItem)cboPickOne.SelectedItem).Content.ToString();
Thanks!
谢谢!
回答by Konstantin Nikolov
I just did this.
我就是这样做的。
string SelectedItem = MyComboBox.Text;
回答by Jim Brissom
If you already know the content of your ComboBoxItem are only going to be strings, just access the content as string:
如果您已经知道 ComboBoxItem 的内容只是字符串,只需将内容作为字符串访问:
string text = ((ComboBoxItem)cboPickOne.SelectedItem).Content.ToString();
回答by Mahmoud
If you add items in ComboBox as
如果您将 ComboBox 中的项目添加为
youComboBox.Items.Add("Data");
Then use this:
然后使用这个:
youComboBox.SelectedItem;
But if you add items by data binding, use this:
但是如果您通过数据绑定添加项目,请使用:
DataRowView vrow = (DataRowView)youComboBox.SelectedItem;
DataRow row = vrow.Row;
MessageBox.Show(row[1].ToString());
回答by Loreno
Using cboPickOne.Text
should give you the string.
使用cboPickOne.Text
应该给你字符串。
回答by Heinzi
var s = (string)((ComboBoxItem)cboPickOne.SelectedItem).Content;
Dim s = DirectCast(DirectCast(cboPickOne.SelectedItem, ComboBoxItem).Content, String)
Since we knowthat the content is a string, I prefer a cast over a ToString()
method call.
因为我们知道内容是一个字符串,所以我更喜欢强制转换而不是ToString()
方法调用。
回答by Nitin Tyagi
Use the DataRowView.Row.Item[Index]
or ItemArray[Index]
property to get the SelectedItem
, where Index is the index of the column in the DataTable
used as itemSource
for the combobox. In your case it will be 0. Instead of index you can also pass the Column name also:
使用DataRowView.Row.Item[Index]
orItemArray[Index]
属性获取SelectedItem
,其中 Index 是DataTable
用于itemSource
组合框的列的索引。在您的情况下,它将是 0。您还可以传递列名而不是索引:
VB:
VB:
Dim sItem As String=DirectCast(cboPickOne.SelectedItem, DataRowView).Row.Item(1).ToString()
C#
C#
String sItem=((DataRowView)cboPickOne.SelectedItem).Row.Item[1].ToString();
To get the SelectedValue you can use:
要获得 SelectedValue,您可以使用:
VB:
VB:
Dim sValue As String=cboPickOne.SelectedValue.ToString()
C#
C#
String sValue=cboPickOne.SelectedValue.ToString();