How to change the itemsSource of a ComboBox in C#

2020-07-26 10:28发布

问题:

I am trying to change the itemsSource of a comboBox at run-time. In this question I was told to do, comboBox.itemssource.... That would be okay if all I needed to do was create a new comboBox and then call the command on it. However, I need to perform this operation on a comboBox that already exists in my User Control through xaml. In that case, how would I reference it? I know how to bind to properties in the control, but in this case I would need to get the whole control. Am I over-thinking it? What is the best way to do what I'm thinking?

This how I am currently switching the Collections in the comboBox (This is all at the model level):

//Property for Combo Box List
public ObservableCollection<string> 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<string>(newList);
        }
    }
}

The collections switch when using this implementation, but the selectedItem in the comboBox doesn't stick. For example, when I click on a different command and then switch back, the box no longer has a selectedItem.

UPDATE

I have a property called selectedOperation that is bound to my comboBox. It contains a simple getter and setter, with a NotifyPropertyChange. This makes it so that the selectedItem in the box stays selected. BUT, if the user clicks on a different command and selects a different item in the comboBox, that new item takes it's place. I need to be able to have a selectedItem for each collection that the comboBox holds.

For example:

Let's say there are 2 commands in the listBox, A and B. Each create a different collection in the comboBox. A creates a collection of numbers, and B creates a collection of names.

For command A the user selects 5. When A is selected the comboBox should display 5 as it's selectedItem. A -> 5

For command B the user selectes Roger. When B is selected the comboBox should display "Roger" as it's selectedItem. B -> Roger

Currently, the comboBox does not remember it's selectedItem when the user switches between commands.

回答1:

I would rather use a DataContext and update that source than manually updating a ComboBox.ItemsSourceproperty.

This way there would be no need to know about the controls at all.

Here is a small example :

When the user clicks the button, you just take care of updating your data, not the controls presenting it.

<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" x:Name="Window1">
    <Grid DataContext="{Binding ElementName=Window1}">
        <StackPanel>
            <Button Click="Button_Click">Some data 1</Button>
            <Button Click="Button_Click_1">Some data 2</Button>
            <ListBox x:Name="ComboBox1" ItemsSource="{Binding Collection}"></ListBox>
        </StackPanel>
    </Grid>
</Window>
using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApplication10
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly ObservableCollection<string> _collection = new ObservableCollection<string>();

        public MainWindow()
        {
            InitializeComponent();
        }

        public ObservableCollection<string> 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);
            }
        }
    }
}

Update

If you want to use a new collection instead of removing items, you will have to implement INotifyPropertyChanged for the collection.

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace WpfApplication10
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private ObservableCollection<string> _collection = new ObservableCollection<string>();

        public MainWindow()
        {
            InitializeComponent();
        }

        public ObservableCollection<string> 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<string>(new[] {"1", "2"});
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Collection = new ObservableCollection<string>(new[] {"3", "4"});
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Note: the [CallerMemberName] saves you from adding the property name each time you invoke the invocator but it's only for .NET 4.5 if I remember correctly.

If you are not under .NET 4.5 then you'll have to put OnPropertyChanged("Collection") instead.

Reference : INotifyPropertyChanged

Also, update Collection with a new collection, not _collection otherwise your UI won't be notified.

EDIT 2

You need to track the selected item according the collection used.

<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" x:Name="Window1">
    <Grid>
        <StackPanel>
            <Button Click="Button_Click">Some data 1</Button>
            <Button Click="Button_Click_1">Some data 2</Button>
            <ListBox x:Name="ComboBox1" ItemsSource="{Binding}" SelectedItem="{Binding MySelectedItem}" />
        </StackPanel>
    </Grid>
</Window>

Code behind :

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<string>(new[] {"a", "b"});
            MyCustomCollection2 = new MyCustomCollection<string>(new[] {"c", "d"});
        }

        public MyCustomCollection<string> MyCustomCollection1 { get; set; }

        public MyCustomCollection<string> 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<T> : ObservableCollection<T>
    {
        private T _mySelectedItem;

        public MyCustomCollection(IEnumerable<T> collection) : base(collection)
        {
        }

        public T MySelectedItem
        {
            get { return _mySelectedItem; }
            set
            {
                if (Equals(value, _mySelectedItem))return;
                _mySelectedItem = value;
                OnPropertyChanged(new PropertyChangedEventArgs("MySelectedItem"));
            }
        }
    }
}


回答2:

try changing the collection via style using some trigger (can be any trigger data/event) here is an example:

<Style x:Key="MySelectItemSourceStyle" TargetType="ComboBox">
    <Setter Property="ItemsSource" Value="{Binding Collection1}" />
    <Style.Triggers>
       <DataTrigger Binding="{Binding SomeValue}" Value="SecondCollection">
          <Setter Property="ItemsSource" Value="{Binding Collection2}" />
       </DataTrigger>
    </Style.Triggers>
</Style>