[英]DisplayMemberPath and SelectedValuePath issue while using LINQ to generate ComboBox.ItemsSource
[英]Issue with ComboBox SelectedValuePath in code behind
我正在嘗試將Dictionary
綁定到 WPF 應用程序中的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 端,值正確綁定。 但是在提交操作中,我只能看到值( Display value
)只有選定值的Key
。
frameWorkElement.SetBinding(ComboBox.SelectedValueProperty, binding);
如果ComboBox
的DataContext
或父元素設置為具有名為“ComboBoxSourceValue”的string
源屬性的類的實例,則應該可以工作。
SelectedValuePath
是指SortedDictionary
KeyValuePair<TKey, TValue>
一個屬性。 您仍然需要將該值綁定到您的源屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.