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

本文关键字:SelectedValue 顶部 类型 绑定 返回 控件 ComboBox | 更新日期: 2023-09-27 18:01:31

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

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

你应该设置SelectedValuePathContent:

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