简体   繁体   English

组合框绑定到字典并检索关联的字典键

[英]Combobox binded to Dictonary and retrieving the associated dictionary key

I have a ComboBox , and I have bound a Dictionary to it. 我有一个ComboBox ,并且已将Dictionary绑定到它。 Here is the code: 这是代码:

<ComboBox Height="24" Name="comboBoxQuery" Width="300" ItemsSource="{Binding QueryNames}" SelectedItem="{Binding SelectedQueryNames}" SelectedValuePath="Key" DisplayMemberPath="Value" Visibility="{Binding Path=ComboVisibility, Converter={StaticResource BoolToVis}}" />

Now, when I try to fetch the selected value, I get a string with key and value together. 现在,当我尝试获取选定的值时,我得到了一个包含键和值的字符串。 Ex: [1, "ABC"] 例如:[1,“ ABC”]

my view model code: 我的视图模型代码:

    public Dictionary<int, string> QueryNames 
    { 
        get 
        { 
            return m_ReadOnlyQueryNames; 
        }
        set
        {
            m_queryNames = value;
            OnPropertyChanged("QueryNames");
        }
    }

    private string m_SelectedQueryNames;        

    public string SelectedQueryNames
    {
        get 
        {
            return m_SelectedQueryNames;
        }            
        set
        {
            if (m_SelectedQueryModule != value) <!-- value returns [1, "abc"]-->
            {
                m_SelectedQueryModule = value;
                OnPropertyChanged("SelectedQueryNames");
            }
        }
    }

If you are using SelectedItem , your property will get the whole object (in you case property will get dictionary element with key and value). 如果使用SelectedItem ,则属性将获取整个对象(在这种情况下,属性将获取具有键和值的字典元素)。 If you want to get just key or just value, try using SelectedValue . 如果只想获取键或值,请尝试使用SelectedValue But it depends on how you set up SelectedValuePath . 但这取决于您如何设置SelectedValuePath

Regards, 问候,

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

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