繁体   English   中英

WPF ListBox绑定未显示

[英]WPF ListBox binding not showing up

我是WPF的新手,并且在将数据绑定到简单的ListBox时遇到了问题。 我在MainWindow.XAML中设置了它

<ListBox Name="lbxShows" />

然后在后面的代码中,我将ItemsSource属性设置为Show对象的Show对象的ObservableCollection。 这实际上是另一个类(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为null,因此似乎没有订阅我的事件。 鉴于这通常会通过绑定自动发生(我认为?),那么我猜测绑定尚未正确设置。

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

您正在做的事情应该足以绑定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;
        }
    }
}

mainApp for ShowList你需要的只是

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

你必须有getters和设定者才能发挥作用。

暂无
暂无

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

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