简体   繁体   English

更改绑定的组合框项目

[英]Changing Bound ComboBox Items

I want to expand on my previous question Pass ComboBox Selected Item as Method Parameter 我想扩展我之前的问题,将ComboBox所选项目作为方法参数

Answer https://stackoverflow.com/a/45703484/6806643 回答https://stackoverflow.com/a/45703484/6806643


I have a ComboBox bound to a List of items. 我有一个绑定到项目列表的ComboBox。

I'd like when a button is pressed to change the ComboBox to a different List of items. 我想按一下按钮将ComboBox更改为其他项目列表。

From: Red, Orange Yellow, Green, Blue, Purple 从: Red, Orange Yellow, Green, Blue, Purple
To: Cyan, Magenta, Yellow, Black 至: Cyan, Magenta, Yellow, Black

The items set when the program starts. 程序启动时设置的项目。 Inside the Change Button I add the new Colors to the _myComboItems List, but how to get it to set again when pressing the button? 在“ Change Button内部,我将新的颜色添加到_myComboItems列表中,但是如何在按下按钮时重新设置它?

程序


XAML XAML

<Window x:Class="MyProgram.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyProgram"
        mc:Ignorable="d"
        Title="MainWindow" Height="289" Width="525">

    <Grid>
        <ComboBox x:Name="comboBox" 
                  DisplayMemberPath="Name"
                  ItemsSource="{Binding MyComboItems}"
                  SelectedItem="{Binding SelectedComboItem}"
                  SelectedIndex="0"
                  HorizontalAlignment="Left" 
                  Margin="264,88,0,0" 
                  VerticalAlignment="Top" 
                  Width="120"/>

        <Button x:Name="buttonChange" 
                Content="Change" 
                HorizontalAlignment="Left" 
                Margin="142,88,0,0" 
                VerticalAlignment="Top" 
                Width="75" 
                Click="button1_Click"/>
    </Grid>
</Window>

C# C#

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        SelectedComboItem = MyComboItems[0];
    }


    // ComboBox Items
    //
    private List<ComboItem> _myComboItems = new List<ComboItem>()
    {
      new ComboItem("Red"),
      new ComboItem("Orange"),
      new ComboItem("Yellow"),
      new ComboItem("Green"),
      new ComboItem("Blue"),
      new ComboItem("Purple")
    };

    public List<ComboItem> MyComboItems
    {
        get { return _myComboItems; }
    }


    // Property Changed
    //
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }


    // Selected Item
    //
    private ComboItem _selected = null;
    public ComboItem SelectedComboItem
    {
        get { return _selected; }
        set
        {
            _selected = value;
            OnPropertyChanged("SelectedComboItem");
        }
    }


    // Change ComboBox Items
    //
    private void buttonChange_Click(object sender, RoutedEventArgs e)
    {
        // Change Items Here
    }
}



public class ComboItem
{
    public string Name { get; private set; }

    public ComboItem(string color)
    {
        Name = color;
    }
}

You could either create a new List<ComboItem> and raise the PropertyChanged event for the MyComboItems property. 您可以创建一个新的List<ComboItem>并引发MyComboItems属性的PropertyChanged事件。 Note that your class must also actually implement the INotifyPropertyChanged interface: 请注意,您的类还必须实际实现INotifyPropertyChanged接口:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        SelectedComboItem = MyComboItems[0];
    }

    private List<ComboItem> _myComboItems = new List<ComboItem>()
    {
      new ComboItem("Red"),
      new ComboItem("Orange"),
      new ComboItem("Yellow"),
      new ComboItem("Green"),
      new ComboItem("Blue"),
      new ComboItem("Purple")
    };

    public List<ComboItem> MyComboItems
    {
        get { return _myComboItems; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private ComboItem _selected = null;
    public ComboItem SelectedComboItem
    {
        get { return _selected; }
        set
        {
            _selected = value;
            OnPropertyChanged("SelectedComboItem");
        }
    }


    private void buttonChange_Click(object sender, RoutedEventArgs e)
    {
        // Change Items Here
        _myComboItems = new List<ComboItem>()
        {
            new ComboItem("Cyan"),
            new ComboItem("Magenta"),
            new ComboItem("Magenta"),
            new ComboItem("Black"),
        };
        OnPropertyChanged("MyComboItems");
        SelectedComboItem = MyComboItems[0];
    }
}

Another option would be to replace the List<ComboItem> with an ObservableCollection<ComboItem> and simply remove and add items to this one: 另一种选择是将List<ComboItem>替换为ObservableCollection<ComboItem>然后简单地删除并添加项目到该对象:

private ObservableCollection<ComboItem> _myComboItems = new ObservableCollection<ComboItem>()
{
    new ComboItem("Red"),
    new ComboItem("Orange"),
    new ComboItem("Yellow"),
    new ComboItem("Green"),
    new ComboItem("Blue"),
    new ComboItem("Purple")
};

public ObservableCollection<ComboItem> MyComboItems
{
    get { return _myComboItems; }
}

private void buttonChange_Click(object sender, RoutedEventArgs e)
{
    _myComboItems.Clear();
    _myComboItems.Add(new ComboItem("Cyan"));
    _myComboItems.Add(new ComboItem("Magenta"));
    _myComboItems.Add(new ComboItem("Magenta"));
    _myComboItems.Add(new ComboItem("Black"));
    SelectedComboItem = MyComboItems[0];
}

The difference between the two collection types is that the latter implements the INotifyCollectionChanged interface. 两种集合类型之间的差异在于后者实现了INotifyCollectionChanged接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM