如何在C#中更改ComboBox的itemsSource
我试图在运行时更改comboBox
的itemsSource
。 在这个问题上,我被告知要做, comboBox.itemssource...
如果我需要做的就是创建一个新的comboBox
然后在其上调用命令,那就没关系。 但是,我需要通过xaml在我的用户控件中已经存在的comboBox
上执行此操作。 在那种情况下,我该如何引用它? 我知道如何绑定到控件中的属性,但在这种情况下,我需要获得整个控件。 我是在过度思考吗? 做我想的最好的方法是什么?
这是我当前在comboBox
切换集合的方式(这是模型级别的全部):
//Property for Combo Box List public ObservableCollection ComboBoxList { get { return _comboBoxList; } set { if (Equals(value, _comboBoxList)) return; _comboBoxList = value; OnPropertyChanged("ComboBoxList"); } } public string SelectedCommand { get { return _selectedCommand; } set { _selectedCommand = value; NotifyPropertyChange(() => SelectedCommand); if (SelectedCommand == "String Value") { ComboBoxList = new ObservableCollection(newList); } } }
使用此实现时,集合会切换,但comboBox
中的selectedItem
不会粘贴。 例如,当我单击其他命令然后切换回来时,该框不再具有selectedItem
。
UPDATE
我有一个名为selectedOperation
的属性绑定到我的comboBox
。 它包含一个简单的getter和setter,带有NotifyPropertyChange
。 这使得框中的selectedItem
保持选中状态。 但是,如果用户单击另一个命令并在组合comboBox
选择一个不同的项目,那么该新项目将取而代之。 我需要能够为comboBox
包含的每个集合都有一个selectedItem
。
例如:
假设listBox
,A和B中有2个命令。每个命令在comboBox
创建一个不同的集合。 A创建数字集合,B创建名称集合。
对于命令A,用户选择5.当选择A时, comboBox
应显示5,因为它是selectedItem
。 A – > 5
对于命令B,用户选择Roger。 当选择B时, comboBox
应显示“Roger”,因为它是selectedItem
。 B – >罗杰
目前,当用户在命令之间切换时, comboBox
不记得它的selectedItem
。
我宁愿使用DataContext
并更新该源,而不是手动更新ComboBox.ItemsSource
属性。
这样就根本不需要了解控件。
这是一个小例子:
当用户单击该按钮时,您只需要更新数据,而不是更新数据。
using System.Collections.ObjectModel; using System.Windows; namespace WpfApplication10 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private readonly ObservableCollection _collection = new ObservableCollection (); public MainWindow() { InitializeComponent(); } public ObservableCollection Collection { get { return _collection; } } private void Button_Click(object sender, RoutedEventArgs e) { _collection.Clear(); for (int i = 0; i < 5; i++) { _collection.Add("method 1 item " + i); } } private void Button_Click_1(object sender, RoutedEventArgs e) { _collection.Clear(); for (int i = 0; i < 5; i++) { _collection.Add("method 2 item " + i); } } } }
更新
如果要使用新集合而不是删除项目,则必须为集合实现INotifyPropertyChanged。
using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows; namespace WpfApplication10 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window, INotifyPropertyChanged { private ObservableCollection _collection = new ObservableCollection (); public MainWindow() { InitializeComponent(); } public ObservableCollection Collection { get { return _collection; } set { if (Equals(value, _collection)) return; _collection = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; private void Button_Click(object sender, RoutedEventArgs e) { Collection = new ObservableCollection (new[] {"1", "2"}); } private void Button_Click_1(object sender, RoutedEventArgs e) { Collection = new ObservableCollection (new[] {"3", "4"}); } protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } } }
注意: [CallerMemberName]
会在每次调用[CallerMemberName]
保存您添加属性名称,但如果我没记错的话,它只适用于.NET 4.5。
如果您不在.NET 4.5下,那么您必须改为使用OnPropertyChanged("Collection")
。
参考: INotifyPropertyChanged
此外,使用新集合更新集合,而不是_collection
否则将不会通知您的UI。
编辑2
您需要根据使用的集合跟踪所选项目。
代码背后:
using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Windows; namespace WpfApplication10 { public partial class MainWindow { public MainWindow() { InitializeComponent(); MyCustomCollection1 = new MyCustomCollection(new[] {"a", "b"}); MyCustomCollection2 = new MyCustomCollection (new[] {"c", "d"}); } public MyCustomCollection MyCustomCollection1 { get; set; } public MyCustomCollection MyCustomCollection2 { get; set; } private void Button_Click(object sender, RoutedEventArgs e) { DataContext = MyCustomCollection1; } private void Button_Click_1(object sender, RoutedEventArgs e) { DataContext = MyCustomCollection2; } } public class MyCustomCollection : ObservableCollection { private T _mySelectedItem; public MyCustomCollection(IEnumerable collection) : base(collection) { } public T MySelectedItem { get { return _mySelectedItem; } set { if (Equals(value, _mySelectedItem))return; _mySelectedItem = value; OnPropertyChanged(new PropertyChangedEventArgs("MySelectedItem")); } } } }
尝试使用一些触发器(可以是任何触发器数据/事件)通过样式更改集合这里是一个示例: