简体   繁体   中英

ComboBox binding returns control type on top of SelectedValue

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM