Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将键获取为字典绑定中的组合框选定项_C#_Wpf_Dictionary_Combobox - Fatal编程技术网

C# 将键获取为字典绑定中的组合框选定项

C# 将键获取为字典绑定中的组合框选定项,c#,wpf,dictionary,combobox,C#,Wpf,Dictionary,Combobox,我有一本字典和组合框绑定在一起。例如,假设字典有如下数据集: {1,item1} {2,item2} 现在,当您选择任何选项时,组合框.SelectedItem应仅获取整数键,而不是值 代码如下: public static Dictionary<int, string> newDict { get; set; } newDict = MTMInteraction.getPlanId(); txtPlanId.ItemsSource = newDict; 公共静态字典newDic

我有一本
字典
组合框
绑定在一起。例如,假设
字典
有如下数据集:

{1,item1}
{2,item2}
现在,当您选择任何选项时,
组合框.SelectedItem
应仅获取整数键,而不是值

代码如下:

public static Dictionary<int, string> newDict { get; set; }
newDict = MTMInteraction.getPlanId();
txtPlanId.ItemsSource = newDict;
公共静态字典newDict{get;set;}
newDict=MTMInteraction.getPlanId();
txtPlanId.ItemsSource=newDict;
XAML代码:

<ComboBox x:Name="txtPlanId" ItemsSource="{Binding newDict}" IsEditable="True" Margin="-2,0,79,3" Text="Enter  ID" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>

而不是
选择editem
使用/properties。将
SelectedValuePath
ComboBox
设置为要获取的属性,并将
SelectedValue
绑定到视图模型中的某个属性

<ComboBox 
    x:Name="txtPlanId" 
    ItemsSource="{Binding newDict}" 
    ... 
    SelectedValuePath="Key"
    SelectedValue="{Binding SomeIntProperty}"/>

或者在code
txtPlanId中。所选的value
应为您提供
KeyValuePair

DisplayMemberPath=“Key”

比你问的要多,但它确实回答了问题(我想)


使用System.ComponentModel;
命名空间绑定字典
{
公共部分类主窗口:窗口,INotifyPropertyChanged
{
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串信息)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(信息));
}
}
专用字节?dlBSkey=1;
私有字典dlBS=newdictionary(){{1,“一”},{2,“二”},{5,“五”};
公共主窗口()
{
初始化组件();
}
公共字典DLBS{get{return DLBS;}}
公共字节?DLBSkey
{
获取{return dlBSkey;}
设置
{
if(dlBSkey==值)返回;
dlBSkey=值;
NotifyPropertyChanged(“DLBSkey”);
}
}
}
}

22个问题,没有一个被接受的答案。没有一个答案对你有用吗?