【问题标题】:How to properly update UserControl combobox's Itemsource?如何正确更新用户控件组合框是 Itemssource?
【发布时间】:2016-05-05 21:48:20
【问题描述】:

我对 WPF 完全陌生,我在使用 ItemsSource 更新时遇到了问题。 我创建了一个带有选项卡的单个主窗口 Metro 应用程序(TabItem(s) as UserControlDataContext="{Binding}"),其中显示了不同的数据/使用了不同的方法。

我发现自己苦苦挣扎的是INotifyPropertyChanged(我无法从类似的示例/问题中理解我的问题的解决方案)界面的概念。我试图使如果在窗口中输入新数据(从UserControl 之一初始化),另一个UserControl(或TabItem)中的ComboBox 将自动更新。这是我所拥有的:

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.xamlComboBox 的设置:

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

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

编辑:Registration.xaml 属性已更改(仍然不起作用):

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

【问题讨论】:

    标签: c# wpf user-controls inotifypropertychanged


    【解决方案1】:

    这是 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
        }
    }
    

    【讨论】:

    • 由于某种原因,它说 CollectionView.PropertyChanged 由于其保护级别而无法访问,并且 +=ComboPropertyChangedHandler() 没有任何论据
    • 确保类和成员是public
    • 我做到了。但它说的是一样的:(
    • 只是为了我绝对确定,这所有的代码都属于一个窗口?因为没有public event PropertyChangedEventHandlerpublic void NotifyPropertyChanged(string propertyName)(如在userlist 类中),NotifyPropertyChanged("List1"); 也会导致错误。
    • 贴了代码,就是不明白Source应该怎么更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多