Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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# &引用;SelectedValue";WPF中comboBox的值正确,但包含名称空间的附加文本_C#_Wpf_Combobox - Fatal编程技术网

C# &引用;SelectedValue";WPF中comboBox的值正确,但包含名称空间的附加文本

C# &引用;SelectedValue";WPF中comboBox的值正确,但包含名称空间的附加文本,c#,wpf,combobox,C#,Wpf,Combobox,WPF中comboBox的“SelectedValue”具有正确的值,但带有命名空间的附加文本。我正在跟踪MVVM,“SelectedValue”绑定到“SelectedImage”属性 如果选择第一个comboBoxItem,其值为“System.Windows.Controls.ComboBox:Firmware Image 1” 如何仅获取选定值? <ComboBox Grid.Row="1" Grid.Column="1"

WPF中comboBox的“SelectedValue”具有正确的值,但带有命名空间的附加文本。我正在跟踪MVVM,“SelectedValue”绑定到“SelectedImage”属性

如果选择第一个comboBoxItem,其值为“System.Windows.Controls.ComboBox:Firmware Image 1

如何仅获取选定值?

            <ComboBox Grid.Row="1"
              Grid.Column="1"
              Width="150"
              Height="30"
              ToolTip="Store Identification"
              Margin="0,2,10,0"
              HorizontalAlignment="Right"
              VerticalAlignment="Top"
              Background="#FF1649A0"
              BorderBrush="White"
              Foreground="White"

              SelectedValue="{Binding SelectedImage}">
            <ComboBox.Items>
                <ComboBoxItem Content="Firmware Image 1 " />
                <ComboBoxItem Content="Firmware Image 2" />
            </ComboBox.Items>
        </ComboBox>

如果将项目绑定到
列表
可观察收集
中,则完成操作

<ComboBox ItemsSource="{Binding Images}" SelectedValue="{Binding SelectedImage}" />

绑定到codebehind中的对象,您可以执行
内容
属性。

使用字符串,就这样:

 <ComboBox Grid.Row="1"
          Width="150"
          Height="30"
          ToolTip="Store Identification"
          Margin="0,2,10,0"
          HorizontalAlignment="Right"
          VerticalAlignment="Top"
          Background="#FF1649A0"
          BorderBrush="White"
          Foreground="White"
           SelectedValue="{Binding SelectedImage}" >
        <ComboBox.Items>
            <sys:String>Firmware Image 1</sys:String>
            <sys:String>Firmware Image 2</sys:String>
        </ComboBox.Items>
    </ComboBox>
<ComboBox SelectedItem="{Binding SelectedImage}">
    <ComboBox.Items>
        <ComboBoxItem Content="DisplayText1" />
        <ComboBoxItem Content="DisplayText2" />
    </ComboBox.Items>
</ComboBox>
//to get the text use: (SelectedImage as ComboBoxItem).Content
public object SelectedImage { get; set; }
 <ComboBox Grid.Row="1"
          Width="150"
          Height="30"
          ToolTip="Store Identification"
          Margin="0,2,10,0"
          HorizontalAlignment="Right"
          VerticalAlignment="Top"
          Background="#FF1649A0"
          BorderBrush="White"
          Foreground="White"
           SelectedValue="{Binding SelectedImage}" >
        <ComboBox.Items>
            <sys:String>Firmware Image 1</sys:String>
            <sys:String>Firmware Image 2</sys:String>
        </ComboBox.Items>
    </ComboBox>
  xmlns:sys="clr-namespace:System;assembly=mscorlib"