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