WPF将ListBox绑定到ObservableCollection(Of MyClass)

时间:2015-05-24 05:25:14

标签: wpf vb.net data-binding listbox

我的主要用户控件UserControl1中有一个公共属性StaticFailingPinListItem As New ObservableCollection(Of MyClass)。

Public Class UserControl1
  Public Property StaticFailingPinListItem As New ObservableCollection(Of MyClass)  

MyClass有两个属性 - PinName& ImageLocation。

Public Class MyClass
  Implements INotifyPropertyChanged
  Public Property PinName
  Public Property ImageLocation

如果我在运行时手动将项添加到集合中,我将能够在列表框中查看PinName属性。但是,如果我稍后添加项目(例如:点击按钮后),它将无效。

这是我的xaml:

    ItemsSource="{Binding ElementName =data2, Path=StaticFailingPinListItem}" DisplayMemberPath="PinName"

那么,问题是如何在填充集合后为列表框进行代码隐藏数据绑定?

1 个答案:

答案 0 :(得分:0)

接口INotifyPropertyChanged必须由包含bonded属性的类实现 在您的情况下,这必须是UserControl1

您需要在UserControl1类型

上实现此界面
Public Class UserControl1
    Implements INotifyPropertyChanged
Public Property StaticFailingPinListItem As New ObservableCollection(Of MyClass) 

单击按钮后添加项目时,将通知控件有关集合中的更改

相关问题