Wpf 绑定到dictionary并检索关联字典键的组合框
我有一个Wpf 绑定到dictionary并检索关联字典键的组合框,wpf,xaml,data-binding,dictionary,combobox,Wpf,Xaml,Data Binding,Dictionary,Combobox,我有一个组合框,我还绑定了一本字典。代码如下: <ComboBox Height="24" Name="comboBoxQuery" Width="300" ItemsSource="{Binding QueryNames}" SelectedItem="{Binding SelectedQueryNames}" SelectedValuePath="Key" DisplayMemberPath="Value" Visibility="{Binding Path=ComboVisibili
组合框
,我还绑定了一本字典
。代码如下:
<ComboBox Height="24" Name="comboBoxQuery" Width="300" ItemsSource="{Binding QueryNames}" SelectedItem="{Binding SelectedQueryNames}" SelectedValuePath="Key" DisplayMemberPath="Value" Visibility="{Binding Path=ComboVisibility, Converter={StaticResource BoolToVis}}" />
现在,当我尝试获取所选的值时,我会得到一个包含key和value的字符串。
例:[1,“ABC”]
我的视图模型代码:
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");
}
}
}
公共字典查询名称
{
得到
{
返回m_ReadOnlyQueryNames;
}
设置
{
m_queryNames=值;
OnPropertyChanged(“QueryNames”);
}
}
私有字符串m_SelectedQueryNames;
公共字符串SelectedQueryNames
{
得到
{
返回m_SelectedQueryNames;
}
设置
{
如果(m_SelectedQueryModule!=值)
{
m_SelectedQueryModule=值;
OnPropertyChanged(“SelectedQueryNames”);
}
}
}
如果使用的是SelectedItem
,则属性将获取整个对象(在您的情况下,属性将获取带有键和值的dictionary元素)。如果您想只获取键或值,请尝试使用SelectedValue
。但这取决于如何设置SelectedValuePath
问候,