繁体   English   中英

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

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

我有一个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代替,但是结果是相同的。 我想要的只是值而不是控件类型。 有什么想法吗?

您应该将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