简体   繁体   English

ComboBox绑定返回SelectedValue顶部的控件类型

[英]ComboBox binding returns control type on top of SelectedValue

I have a ComboBox : 我有一个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 : ...绑定到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: 当我输出value ,而不是显示类似以下内容:

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. 我尝试使用SelectedItem代替,但是结果是相同的。 All I want is the value and not the control type. 我想要的只是值而不是控件类型。 Any ideas what's going on? 有什么想法吗?

You should set SelectedValuePath to Content : 您应该将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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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