WPF 列表框绑定未显示

本文关键字:显示 绑定 列表 WPF | 更新日期: 2023-09-27 18:18:01

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

<ListBox Name="lbxShows" />

然后在代码隐藏中,我将 ItemsSource 属性设置为名为 ShowList 的 Show 对象的可观察集合。这实际上是另一个类(Oasis(的属性,OasisInst是其中一个实例(设置在MainApplication的构造函数中(。

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

此时,ShowList 中没有项,但稍后会添加一些项,并且它们不会显示在 ListBox 中。

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

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

是我的 PropertyChangedEventHandler 事件。

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

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

WPF 列表框绑定未显示

ShowList mainApp所需要的只是

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

您必须拥有getters和二传手才能正常工作。

您正在做的事情应该足以绑定 ObservableCollection 并让 U/I 接收更新。我以为不是这样建议使用绑定对象,但发现它可以工作。(所以我也学到了一些东西(。下面是一个简单的示例,它应该适用于你提供的 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;
        }
    }
}