如何正确更新UserControl组合框的Itemsource?

时间:2016-05-05 21:48:20

标签: c# wpf user-controls inotifypropertychanged

我是WPF的新手,我遇到了ItemsSource更新的问题。 我创建了一个主窗口Metro应用程序,其中包含选项卡(TabItem(s)为UserControl DataContext="{Binding}"),其中显示了不同的数据/使用了不同的方法。

我发现自己正在努力的是INotifyPropertyChanged(我无法从类似的例子/问题中理解我的问题的解决方案)界面的概念。我想尝试在窗口中输入新数据(从UserControl之一初始化),另一个ComboBox中的UserControl(或TabItem )会自动更新。这就是我所拥有的:

UserControl1.xaml

 public partial class UserControl1: UserControl
{
    private userlist addlist;
    public UserControl1()
    {
        InitializeComponent();
        fillcombo();
    }
      public void fillcombo()
     {
         Fillfromdb F = new Fillfromdb(); // class that simply connects 
         // to a database sets a datatable as ListCollectionView
         addlist = new addlist { List = F.returnlistview() }; // returns ListCollectionView
         UsersCombo.ItemsSource = addlist.List;
     }

userlist.cs

    public class userlist: INotifyPropertyChanged
   {
    private ListCollectionView _list;
    public ListCollectionView List
    {
        get { return this._list; }
        set
        {
            if (this._list!= value)
            {
                this._list= value;
                this.NotifyPropertyChanged("List");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
   }

Registration.xaml (从另一个UserControl调用)

 public partial class Registration: MetroWindow
{
    public Registration()
    {
        InitializeComponent();
    } 
    private void confirm_button_click(object sender, RoutedEventArgs e)
    {
         // new user is saved to database
         // * here is where I don't know what to do, how to update the ItemSource

    }
   }

以下是 UserControl.xaml 中的ComboBox设置:

<ComboBox x:Name="UsersCombo" 
 ItemsSource="{Binding List, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>

由于我没有任何编程教育/经验,因此非常感谢非常普遍的建议/解释。

编辑: Registration.xaml with propertychanged(仍然无效):

 public partial class Registration : MetroWindow
{
    public userlist instance = new userlist();
    public ListCollectionView _list1;
    public ListCollectionView List1
    {
        get { return this._list1; }
        set
        {
            if (this._list1 != value)
            {
                this._list1 = value;
                this.NotifyPropertyChanged("List1");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }


    public Registration()
    {
        InitializeComponent();
        instance.List.PropertyChanged += ComboPropertyChangedHandler();
   }
    private void confirm_button_click(object sender, RoutedEventArgs e)
    {
         // new user is save to database
         // still don't now what to do with new ListCollectionView from database
    }
    public void ComboPropertyChangedHandler(object obj)
    {
        List1 = instance.List; // when new data from database should be loaded?
    }

1 个答案:

答案 0 :(得分:0)

这是PropertyChanged事件派上用场的地方。 将第二个xaml页面中的组合框绑定到List并创建类似于第一个xaml的类似属性。

在第二个xaml.cs

public partial class Registration: MetroWindow, INotifyPropertyChanged
{
    private userlist instance = new userlist();
    private ListCollectionView _list1;
    public ListCollectionView List1
    {
        get { return this._list1; }
        set
    {
        if (this._list1 != value)
        {
            this._list1 = value;
            this.NotifyPropertyChanged("List1");
        }
    }
}
public Registration()
{
    InitializeComponent();
    instance.List.PropertyChanged += ComboPropertyChangedHandler();
} 

private void ComboPropertyChangedHandler(object obj)
{
    List1 = instance.List;
    //or iterate through the list and add as below
    foreach(var item in instance.List)
    {
        List1.Add(item);
    }
}
private void confirm_button_click(object sender, RoutedEventArgs e)
    {
         // new user is saved to database
         // * here is where I don't know what to do, how to update the ItemSource
    }
}
相关问题