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

时间:2018-07-14 08:48:09

标签: c# wpf mvvm combobox

我有一个组合框cbo1

我正在尝试使用ViewModelCollectionChanged来更改项目来源,但是ComboBox项保持空白并且不会更新。

我在这里尝试了几个示例和解决方案,但不知道如何正确实施它们。


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类

绑定组合框项目

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

}

示例类

在本课程中,我想更改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"
};

1 个答案:

答案 0 :(得分:1)

实现-属性声明中的RaisePropertyChanged(“ ComboBoxItemsource”); / NotifyPropertyChanged(“ ComboBoxItemsource”)。 例如:-

在视图中

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

在视图模型中

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

类应继承并实现INotifyPropertyChanged。 希望对您有所帮助。