【发布时间】:2016-08-23 05:19:18
【问题描述】:
我在 WPF 中动态生成一个组合框,现在,我想用 xaml 资源文件中的动态值填充组合框,组合框中要填充的数据存在于两个不同的 xaml 资源文件中,我想根据用户在应用程序中选择的语言填充组合框。所以,这里的“ItemsSource”属性应该是动态的,取决于选择的语言
我正在尝试使用“SetResourceReference”
cmbCtrl = new ComboBox();
((ComboBox)cmbCtrl).SetResourceReference(ComboBox.ItemsSourceProperty, "to be assigned");
cmbCtrl.DisplayMemberPath = "Value";
cmbCtrl.SelectedValuePath = "Key";
cmbCtrl.ItemsSource = //should be dynamic based on the language selected
cmbCtrl.SelectedIndex = 0;
另外,希望您对如何将组合框中的数据放入 xaml 资源文件提出建议
任何建议都非常感谢,在此先感谢!
【问题讨论】:
-
将 ItemsSource 绑定到
ObservableCollection<string>并在每次语言更改时清除列表并填充它。 -
Cmb.ItemsSource = this.FindResource(...) as SomeCollection.