Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在组合框中获取选定的文本或值_C#_Wpf - Fatal编程技术网

C# 无法在组合框中获取选定的文本或值

C# 无法在组合框中获取选定的文本或值,c#,wpf,C#,Wpf,我的wpf应用程序中有一个combox,其中包含三项: <ComboBoxItem Tag="some value">Text</ComboBoxItem> <ComboBoxItem Tag="some value2">Text2</ComboBoxItem> <ComboBoxItem Tag="some value3">Text3</ComboBoxItem> 它返回以下内容: System.Windows.Cont

我的wpf应用程序中有一个combox,其中包含三项:

<ComboBoxItem Tag="some value">Text</ComboBoxItem>
<ComboBoxItem Tag="some value2">Text2</ComboBoxItem>
<ComboBoxItem Tag="some value3">Text3</ComboBoxItem>
它返回以下内容:

System.Windows.Controls.ComboBoxItem: Text2

如何获取选定的文本或值?

您需要将组合框的SelectedItem属性强制转换为对象,然后才能访问这些属性。因此,在您的情况下,您需要将其强制转换为ComboBoxItem。

因为您需要
ComboxItem的
内容
属性,您应该这样尝试:

(myComboBox.SelectedValue as ComboBoxItem).Content.ToString();
或对于
标记

(myComboBox.SelectedValue as ComboBoxItem).Tag.ToString();
试试这个

<ComboBox Name="comboBox" SelectedValuePath="Content">
  <ComboBoxItem>text</ComboBoxItem>
  <ComboBoxItem>here</ComboBoxItem>
  <ComboBoxItem>text</ComboBoxItem>
</ComboBox>

您只需要从对象获取标记

<ComboBox SelectionChanged="Selector_OnSelectionChanged">
    <ComboBoxItem Tag="some value">Text</ComboBoxItem>
    <ComboBoxItem Tag="some value2">Text2</ComboBoxItem>
    <ComboBoxItem Tag="some value3">Text3</ComboBoxItem>
</ComboBox>

    private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var cb = sender as ComboBox;
        if (cb == null)
        {
            return;
        }

        var selectedItem = cb.SelectedValue as ComboBoxItem;
        if (selectedItem == null)
        {
            return;
        }

        var tag = selectedItem.Tag;

        Debug.WriteLine(tag);  
    }

正文
文本2
文本3
私有无效选择器\u OnSelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
var cb=发送方作为组合框;
如果(cb==null)
{
返回;
}
var selectedItem=cb.SelectedValue作为ComboBoxItem;
如果(selectedItem==null)
{
返回;
}
var tag=selectedItem.tag;
Debug.WriteLine(标记);
}
((YourType)组合框。选择EdItem)。YourProperty
 var value = comboBox.SelectedValue.ToString()
<ComboBox SelectionChanged="Selector_OnSelectionChanged">
    <ComboBoxItem Tag="some value">Text</ComboBoxItem>
    <ComboBoxItem Tag="some value2">Text2</ComboBoxItem>
    <ComboBoxItem Tag="some value3">Text3</ComboBoxItem>
</ComboBox>

    private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var cb = sender as ComboBox;
        if (cb == null)
        {
            return;
        }

        var selectedItem = cb.SelectedValue as ComboBoxItem;
        if (selectedItem == null)
        {
            return;
        }

        var tag = selectedItem.Tag;

        Debug.WriteLine(tag);  
    }