简体   繁体   English

更改ComboBox项源并使用CollectionChanged更新通知

[英]Change ComboBox Item Source and Update Notify with CollectionChanged

I have a ComboBox cbo1 . 我有一个ComboBox cbo1

I'm trying to change the item source using the ViewModel with CollectionChanged but the ComboBox items stay blank and won't update. 我正在尝试使用带有CollectionChangedViewModel更改项目源,但是ComboBox项目保持空白并且不会更新。

I've tried several examples and solutions here, but don't know how to implement them right. 我在这里尝试了几个示例和解决方案,但不知道如何正确实施它们。


XAML XAML

<ComboBox x:Name="cbo1" 
          ItemsSource="{Binding cbo1_Items, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          SelectedItem="{Binding cbo1_SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
          HorizontalAlignment="Left" 
          Margin="0,0,0,0"
          VerticalAlignment="Top"
          Width="105" 
          Height="22" />

ViewModelBase Class ViewModelBase类

Bind ComboBox Items 绑定组合框项目

public class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void Notify(string propName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    public ViewModelBase()
    {
        _cbo1_Items = new ObservableCollection<string>();
        _cbo1_Items.CollectionChanged += cbo1_Items_CollectionChanged;
    }

    // Notify Collection Changed
    //
    public void cbo1_Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Notify("cbo1_Items");
    }


    // Item Source
    //
    public static ObservableCollection<string> _cbo1_Items = new ObservableCollection<string>();
    public static ObservableCollection<string> cbo1_Items
    {
        get { return _cbo1_Items; }
        set { _cbo1_Items = value; }
    }

    // Selected Item
    //
    public static string cbo1_SelectedItem { get; set; }

}

Example Class 示例类

In this class I want to change the ComboBox Item Source. 在此课程中,我想更改ComboBox项源。

// Change ViewModel Item Source
//
ViewModelBase._cbo1_Items = new ObservableCollection<string>()
{
    "Item 1",
    "Item 2",
    "Item 3"
};

// ...

// Change Item Source Again
//
ViewModelBase._cbo1_Items = new ObservableCollection<string>()
{
    "Item 4",
    "Item 5",
    "Item 6"
};

Implement - RaisePropertyChanged("ComboBoxItemsource");/NotifyPropertyChanged("ComboBoxItemsource") in your property declaration. 实施-属性声明中的RaisePropertyChanged(“ ComboBoxItemsource”); / NotifyPropertyChanged(“ ComboBoxItemsource”)。 Ex: - 例如:-

In View 视野中

<ComboBox Width="40" Height="40" ItemsSource="{Binding ComboBoxItemsource, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

In View Model- 在视图模型中

private ObservableCollection<string> comboBoxItemsource;
 public ObservableCollection<string> ComboBoxItemsource
        {
            get { return comboBoxItemsource; }
            set
            {
                if (comboBoxItemsource != value)
                {
                    comboBoxItemsource = value; 
                    RaisePropertyChanged("ComboBoxItemsource");
                }
            }
        }

    In Class Constructor-

public ClassViewModel()
        {
            ComboBoxItemsource = new ObservableCollection<string>();
            ComboBoxItemsource.Add("Item1");
            ComboBoxItemsource.Add("Item2");
           ....
       }

    //Event on which you want to change the collection

    public void OnClickEvent()
    {
                ComboBoxItemsource = new ObservableCollection<string>();
                ComboBoxItemsource.Add("Item5");
                ComboBoxItemsource.Add("Item6");
    }

Class should Inherit and Implement INotifyPropertyChanged. 类应该继承并实现INotifyPropertyChanged。 Hope this Helps.. 希望这可以帮助..

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

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