代码隐藏中 ComboBox SelectedValuePath 的问题

我正在尝试在 WPF 应用程序中将a 绑定Dictionary到我的。ComboBox


SortedDictionary<string, string> result = new SortedDictionary<string, string>();


((ComboBox)frameWorkElement).ItemsSource = result;

((ComboBox)frameWorkElement).DisplayMemberPath = "Value";

((ComboBox)frameWorkElement).SelectedValuePath = "Key";



((ComboBox)frameWorkElement).MinWidth = 200;

frameWorkElement.Name = "ListOfValues";

var binding = new Binding("ComboBoxSourceValue")

{

    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged

};

binding.Mode = BindingMode.TwoWay;

binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;


frameWorkElement.SetBinding(ComboBox.TextProperty, (BindingBase)binding);

在 UI 方面,值已正确绑定。但在提交操作中,我只能看到 value ( Display value) 以及所选值的Key。


幕布斯6054654
浏览 115回答 1
1回答

慕的地6264312

frameWorkElement.SetBinding(ComboBox.SelectedValueProperty,&nbsp;binding);DataContext如果的ComboBox或父元素被设置为具有名为“ComboBoxSourceValue”的源属性的类的实例,则应该可以工作string。SelectedValuePathKeyValuePair<TKey, TValue>指的是中的属性SortedDictionary。您仍然需要将该值绑定到源属性。
打开App,查看更多内容
随时随地看视频慕课网APP