Ahsan Ahsan - 1 month ago 19
C# Question

"SelectedValue" of comboBox in WPF has correct value but with aditional text of namespace

"SelectedValue" of comboBox in WPF has correct value but with aditional text of namespace. I am following MVVM, "SelectedValue" is binded to "SelectedImage" property.

If I select first comboBoxItem, its value is "System.Windows.Controls.ComboBox:Firmware Image 1".

How to get only selected value?

<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>

Rom Rom
Answer

Use strings, there you go:

 <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>

Add the namespace:

  xmlns:sys="clr-namespace:System;assembly=mscorlib"