【问题标题】:WPF ListBox binding not showing upWPF ListBox 绑定未显示
【发布时间】:2017-03-30 00:13:49
【问题描述】:

我是 WPF 新手,在将数据绑定到简单列表框时遇到问题。我已经在 MainWindow.XAML 中设置了

<ListBox Name="lbxShows" />

然后在后面的代码中,我将 ItemsSource 属性设置为称为 ShowList 的 Show 对象的 ObservableCollection。这实际上是另一个类(Oasis)的属性,OasisInst 是该类的一个实例(在 MainApplication 的构造函数中设置)。

InitializeComponent();
mainApp = new MainApplication();
lbxShows.ItemsSource = mainApp.OasisInst.ShowList;

此时,ShowList 中没有项目,但后来添加了一些项目,它们不会出现在 ListBox 中。

Oasis 类的代码实现了 INotifyPropertyChanged 接口,然后具有从 ShowList 设置器调用的教科书方法。

private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

PropertyChanged 是我的 PropertyChangedEventHandler 事件。

当我在调试模式下单步执行时,PropertyChanged 为 null,因此似乎没有任何内容订阅我的事件。鉴于这通常会通过绑定自动发生(我认为?),那么我猜测绑定没有正确设置。

也许仅设置 ItemsSource 属性不足以设置绑定?

【问题讨论】:

  • “Oasis 类的代码实现了 INotifyPropertyChanged 接口”——只要不更改ShowList 属性的值,这无关紧要。如果您确实更改了ShowList 属性的值,即创建一个全新的集合并将属性值设置为引用该集合,那么仅仅实现接口是不够的。您需要将属性实际绑定 到目标ItemsSource 属性,而不仅仅是设置它。通常这将在 XAML 中完成,但如果您坚持也可以在代码隐藏中完成。
  • 您的问题是无法回答的,因为没有好的minimal reproducible example 就不可能确切地知道代码为什么不起作用。请解决您的问题。

标签: c# wpf xaml data-binding listbox


【解决方案1】:

mainApp ShowList 所需要的只是

public ObservableCollection<string> ShowList {get; set;}

您必须拥有 getters 和 setter` 才能使其工作。

【讨论】:

    【解决方案2】:

    您所做的应该足以绑定 ObservableCollection 并让 U/I 接收更新。 我认为这不是建议使用 BindingObject 但发现它有效。 (所以我也学到了一些东西)。 这是一个应该与您提供的 Xaml 一起使用的简单示例。它每秒在列表中添加一个条目。

    我很困惑你提到“PropertyChanged 是我的 PropertyChangedEventHandler 事件”。请注意,唯一需要的 PropertyChangedEventHandler 在 Oasis 对象内。

    也许您的意思是您的 U/I 上有其他控件,需要在 MainWindow 上使用 PropertyChangedEventHandler,但不应与 Oasis 对象内的 PropertyChangedEventHandler 交互。

    ----下面的代码----

    using System;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Threading;
    
    namespace ListBoxTest
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        /// 
    
        public class OasisInstance : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            ObservableCollection<string> _items = new ObservableCollection<string>();
            public ObservableCollection<string> ShowList
            {
                get { return _items; }
                set {
                    if (_items != value)
                    {
                        _items = value; NotifyPropertyChanged();
                    }
                }
            }
    
            private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    
        public class MainApplication
        {
            public OasisInstance OasisInst  = new OasisInstance();
        }
    
        public partial class MainWindow : Window
        {
            MainApplication mainApp = new MainApplication();
            DispatcherTimer timer = new DispatcherTimer();
    
            public MainWindow()
            {
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += (s, e) => { mainApp.OasisInst.ShowList.Add(DateTime.Now.ToString()); };
                timer.Start();
    
                InitializeComponent();
    
                lbxShows.ItemsSource = mainApp.OasisInst.ShowList;
            }
        }
    }
    

    【讨论】:

    • 非常感谢您花时间回复,这真的很有帮助。我现在已经开始工作了。有一些问题。如上所述,一件事是理解对集合的引用的概念,而不是集合项目本身。此外,在我将 ListBox 的 ItemsSource 属性设置为 ShowList 时,我实际上并没有设置集合,因此 ShowList 为空。虽然后来创建并填充了该集合,但 DataSource 没有引用它。我通过创建一个空集合来纠正它,然后分配 ItemSource,然后再填充它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 2010-12-24
    • 2014-05-02
    • 2018-12-18
    • 1970-01-01
    相关资源
    最近更新 更多