更改绑定的ComboBox项

时间:2017-08-17 05:42:21

标签: c# wpf xaml combobox

我想扩展我之前的问题 Pass ComboBox Selected Item as Method Parameter

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

我有一个ComboBox绑定到一个项目列表。

我想按下按钮将ComboBox更改为不同的项目列表。

来自:Red, Orange Yellow, Green, Blue, Purple
收件人:Cyan, Magenta, Yellow, Black

程序启动时设置的项目。在Change Button内,我将新颜色添加到_myComboItems列表,但是如何在按下按钮时再次设置它?

Program

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#

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;
    }
}

1 个答案:

答案 0 :(得分:2)

您可以创建新的List<ComboItem>并为PropertyChanged属性举起MyComboItems事件。请注意,您的类还必须实际实现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];
    }
}

另一种选择是将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];
}

两种集合类型之间的区别在于后者实现了INotifyCollectionChanged接口。