ComboBox绑定在SelectedValue之上返回控件类型

时间:2014-05-04 16:27:04

标签: c# wpf binding combobox propertychanged

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

...绑定到Property

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

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

当我输出value时,而不是显示如下内容:

Bug Report
Suggestion

......我明白了:

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

我尝试使用SelectedItem代替,但结果是一样的。我想要的只是价值而不是控制类型。有什么想法会发生什么?

1 个答案:

答案 0 :(得分:2)

您应该将SelectedValuePath设置为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>