更改绑定的组合框项目 [英] Changing Bound ComboBox Items

查看:58
本文介绍了更改绑定的组合框项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展我的上一个问题






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
Horizo​​ntalAlignment = Left
Margin = 264,88,0,0
VerticalAlignment = Top
Width = 120 /> ;

<按钮x:Name = buttonChange
Content = Change
Horizo​​ntalAlignment = Left
Margin = 142,88,0,0
VerticalAlignment =顶部
宽度= 75
Click = button1_Click />
< / Grid>
< / Window>

C#

 公共子类MainWindow:窗口
{
public MainWindow()
{
InitializeComponent();

DataContext = this;

SelectedComboItem = MyComboItems [0];
}


// ComboBox项目
//
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)
};

公共列表< ComboItem> MyComboItems
{
get {return _myComboItems; }
}


//属性已更改
//
公共事件PropertyChangedEventHandler PropertyChanged;

私有无效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)
{
//在此处更改项目
}
}



公共类ComboItem
{
public字符串名称{get;私人套装; }

public ComboItem(字符串颜色)
{
Name = color;
}
}


解决方案

您可以创建一个新的 List< ComboItem> 并为 MyComboItems <引发 PropertyChanged 事件/ code>属性。请注意,您的类还必须实际实现 INotifyPropertyChanged 接口:

  public局部类MainWindow:Window,INotifyPropertyChanged 
{
public MainWindow()
{
InitializeComponent();

DataContext = this;

SelectedComboItem = MyComboItems [0];
}

私人列表< ComboItem> _myComboItems = new List< ComboItem>()
{
new ComboItem( Red),
new ComboItem( Orange),
new ComboItem( Yellow),
new ComboItem( Green),
new ComboItem( Blue),
new ComboItem( Purple)
};

公共列表< ComboItem> MyComboItems
{
get {return _myComboItems; }
}

公共事件PropertyChangedEventHandler PropertyChanged;

私有无效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)
{
//在此处更改项目_myComboItems =新列表< ComboItem>()
{
新ComboItem(青色),
新ComboItem(品红色),
新ComboItem(品红色),
new ComboItem( Black),
};
OnPropertyChanged( MyComboItems);
SelectedComboItem = MyComboItems [0];
}
}

另一种选择是替换具有 ObservableCollection< ComboItem> 的List< 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(对象发送者,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 接口。


I want to expand on my previous question Pass ComboBox Selected Item as Method Parameter

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


I have a ComboBox bound to a List of items.

I'd like when a button is pressed to change the ComboBox to a different List of items.

From: Red, Orange Yellow, Green, Blue, Purple
To: 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?


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

解决方案

You could either create a new List<ComboItem> and raise the PropertyChanged event for the MyComboItems property. Note that your class must also actually implement the INotifyPropertyChanged interface:

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:

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.

这篇关于更改绑定的组合框项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆