Question

I have a ComboBox:

<ComboBox Grid.Column="1" Grid.Row="9" SelectedValue="{Binding SelectedReason}">
    <ComboBoxItem Content="Bug Report" IsSelected="True"/>
    <ComboBoxItem Content="Suggestion"/>
    <ComboBoxItem Content="Complaint"/>
    <ComboBoxItem Content="Other"/>
</ComboBox>

...which binds to a Property:

private string _selectedReason;
public string SelectedReason
{
    get { return _selectedReason; }
    set
    {
        if (_selectedReason == value)
        {
            return;
        }

        _selectedReason = value;
        OnPropertyChanged("SelectedReason");
    }
}

When I output the value, instead of showing something like:

Bug Report
Suggestion

...I get:

System.Windows.Controls.ComboBoxItem: Bug Report
System.Windows.Controls.ComboBoxItem: Suggestion

I tried using SelectedItem instead, but the result is the same. All I want is the value and not the control type. Any ideas what's going on?

Was it helpful?

Solution

You should set SelectedValuePath to Content :

<ComboBox Grid.Column="1" Grid.Row="9" SelectedValue="{Binding SelectedReason}" 
        SelectedValuePath="Content">
    <ComboBoxItem Content="Bug Report" IsSelected="True"/>
    <ComboBoxItem Content="Suggestion"/>
    <ComboBoxItem Content="Complaint"/>
    <ComboBoxItem Content="Other"/>
</ComboBox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top