【问题标题】:Get Selected Field off a value on a ComboBox bound to ObservableDictionary从绑定到 ObservableDictionary 的 ComboBox 上的值中获取选定字段
【发布时间】:2018-01-24 17:27:46
【问题描述】:

这很尴尬。

我正在尝试将 ObservableDictionary 绑定到 ComboBox。

我已经完成了这项工作,但我进一步将选定的值绑定到一个属性,但我没有得到正确的字段值。

这是我的模特

public class Category
{
    public Int64 CategoryId { get; set; }
    public string CategoryRef { get; set; }
    public string Description { get; set; }
}

这是我的 ViewModel:

public Dictionary<string, Category> _categoryFields;

public Dictionary<string, Category> CategoryFields
{
    get => _categoryFields;
    set
    {
        _categoryFields = value;
        RaisePropertyChanged("CategoryFields");
    }
}

private string _categorySelected;

public string CategorySelected
{
    get => _categorySelected;
    set
    {
        _categorySelected= value;
        RaisePropertyChanged("CategorySelected");
        EvJobCategoryHasChanged?.Invoke();
    }
}

这是我的观点:

<ComboBoxAdv 
    DisplayMemberPath="Value.Description"                                         
    SelectedValue="{Binding CategorySelected,Mode=TwoWay}"                                         
    SelectedValuePath="{Binding Value.Description}"                                        
    ItemsSource="{Binding CategoryFields}" />

我正在尝试获取用户选择的描述。

【问题讨论】:

    标签: c# wpf combobox observablecollection model-binding


    【解决方案1】:

    这可能是你的问题:

    SelectedValuePath="{Binding Value.Description}"
    

    这个属性不需要绑定;它应该是一个属性路径,就像DisplayMemberPath。改成:

    SelectedValuePath="Value.Description"
    

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 2020-05-17
      相关资源
      最近更新 更多