簡體   English   中英

隱藏代碼中的 ComboBox SelectedValuePath 問題

[英]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);

如果ComboBoxDataContext或父元素設置為具有名為“ComboBoxSourceValue”的string源屬性的類的實例,則應該可以工作。

SelectedValuePath是指SortedDictionary KeyValuePair<TKey, TValue>一個屬性。 您仍然需要將該值綁定到您的源屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM