C# WPF列表框绑定未显示
我是WPF新手,在将数据绑定到简单列表框时遇到问题。我已经在MainWindow.XAML中设置了它C# WPF列表框绑定未显示,c#,wpf,xaml,data-binding,listbox,C#,Wpf,Xaml,Data Binding,Listbox,我是WPF新手,在将数据绑定到简单列表框时遇到问题。我已经在MainWindow.XAML中设置了它 <ListBox Name="lbxShows" /> 此时,ShowList中没有项目,但稍后会添加一些项目,并且它们不会显示在列表框中 Oasis类的代码实现INotifyPropertyChanged接口,然后具有从ShowList setter调用的教科书方法 private void NotifyPropertyChanged([CallerMemberName] str
<ListBox Name="lbxShows" />
此时,ShowList中没有项目,但稍后会添加一些项目,并且它们不会显示在列表框中
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上有其他控件,需要在主窗口上安装PropertyChangedEventHandler,但不应与Oasis对象内的PropertyChangedEventHandler交互 ----代码如下--
使用系统;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Runtime.CompilerServices;
使用System.Windows;
使用System.Windows.Threading;
名称空间ListBoxTest
{
///
///MainWindow.xaml的交互逻辑
///
///
公共类OASIInstance:InotifyProperty已更改
{
公共事件属性更改事件处理程序属性更改;
ObservableCollection_items=新的ObservableCollection();
公共可观测集合显示列表
{
获取{return\u items;}
设置{
如果(_items!=值)
{
_items=值;NotifyPropertyChanged();
}
}
}
私有void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
公共类应用程序
{
公共OASIInstance OASIInstance=新OASIInstance();
}
公共部分类主窗口:窗口
{
MainApplication mainApp=新的MainApplication();
调度程序计时器=新调度程序();
公共主窗口()
{
timer.Interval=TimeSpan.FromSeconds(1);
timer.Tick+=(s,e)=>{mainApp.oasisnst.ShowList.Add(DateTime.Now.ToString());};
timer.Start();
初始化组件();
lbxShows.ItemsSource=mainApp.oasisnst.ShowList;
}
}
}
您在主应用程序中所需的显示列表
是
public ObservableCollection<string> ShowList {get; set;}
publicobservableCollection显示列表{get;set;}
必须有getter
和setter`才能工作。“Oasis类的代码实现INotifyPropertyChanged接口”--只要不更改ShowList
属性的值,这是无关的。如果确实更改了ShowList
属性的值,即创建一个全新的集合并将属性值设置为引用该集合,则仅实现该接口是不够的。您需要将属性实际绑定到目标ItemsSource
属性,而不仅仅是设置它。通常这将在XAML中完成,但如果您坚持的话,也可以在代码隐藏中完成。您的问题不可回答,因为如果没有一个好的解决方案,就不可能确切地知道代码为什么不工作。请修正您的问题。非常感谢您抽出时间回答,这真的很有帮助。我现在让它工作了。有一些问题。如上所述,有一件事是理解集合引用的概念相对于集合项本身发生了变化。另外,在我将ListBox的ItemsSource属性设置为ShowList时,我实际上没有设置集合,因此ShowList为null。尽管后来创建并填充了该集合,但DataSource没有对其进行引用。我通过创建一个空集合,然后分配ItemSource,然后填充来纠正它。
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;
}
}
}
public ObservableCollection<string> ShowList {get; set;}