简体   繁体   English

WPF ListBox绑定未显示

[英]WPF ListBox binding not showing up

I'm new to WPF and am having problems binding data to a simple ListBox. 我是WPF的新手,并且在将数据绑定到简单的ListBox时遇到了问题。 I've set it up in MainWindow.XAML 我在MainWindow.XAML中设置了它

<ListBox Name="lbxShows" />

then in the code behind, I set the ItemsSource property to be an ObservableCollection of Show objects called ShowList. 然后在后面的代码中,我将ItemsSource属性设置为Show对象的Show对象的ObservableCollection。 This is actually a property of another class (Oasis) of which OasisInst is an instance (setup in the constructor of MainApplication). 这实际上是另一个类(Oasis)的属性,其中OasisInst是一个实例(在MainApplication的构造函数中设置)。

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

At this point, there are no items in ShowList but later, some get added and they don't appear in the ListBox. 此时,ShowList中没有项目,但稍后会添加一些项目,并且它们不会出现在ListBox中。

The code for the Oasis class implements the INotifyPropertyChanged interface and then has the textbook method that's called from the ShowList setter. Oasis类的代码实现了INotifyPropertyChanged接口,然后具有从ShowList setter调用的教科书方法。

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

PropertyChanged is my PropertyChangedEventHandler event. PropertyChanged是我的PropertyChangedEventHandler事件。

When I step through in debug mode, PropertyChanged is null so it seems that nothing has subscribed to my event. 当我在调试模式中单步执行时,PropertyChanged为null,因此似乎没有订阅我的事件。 Given that this would usually happen automatically through a binding (I think?), then I'm guessing the binding hasn't been setup properly. 鉴于这通常会通过绑定自动发生(我认为?),那么我猜测绑定尚未正确设置。

Maybe setting the ItemsSource property alone isn't sufficient to setup the binding? 也许单独设置ItemsSource属性不足以设置绑定?

What you are doing should be enough to bind the ObservableCollection and have the U/I receive updates. 您正在做的事情应该足以绑定ObservableCollection并使U / I接收更新。 I thought it was not so was going to suggest using a BindingObject but found it works. 我认为不是这样建议使用BindingObject但发现它有效。 (So I too learned something). (所以我也学到了一些东西)。 Here is a simple example that should work with the Xaml you provided. 这是一个应该与您提供的Xaml一起使用的简单示例。 It adds an entry in the list once per second. 它每秒在列表中添加一个条目。

I am confused where you mention "PropertyChanged is my PropertyChangedEventHandler event." 我很困惑,你提到“PropertyChanged是我的PropertyChangedEventHandler事件。” Note that the only PropertyChangedEventHandler required is inside the Oasis object. 请注意,唯一需要的PropertyChangedEventHandler位于Oasis对象中。

Perhaps you mean you have other controls on your U/I that need a PropertyChangedEventHandler on your MainWindow but that should have no interaction with the PropertyChangedEventHandler inside the Oasis object. 也许你的意思是你的U / I上有其他控件需要MainWindow上的PropertyChangedEventHandler,但是它不应该与Oasis对象内的PropertyChangedEventHandler交互。

---- code below ---- ----下面的代码----

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

All you need in the mainApp for ShowList is mainApp for ShowList你需要的只是

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

You MUST have the getters and setters` for it to work. 你必须有getters和设定者才能发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM