很抱歉,如果这是一个重复的问题。围绕这个主题有很多问题,但我似乎无法弄明白。我正在尝试将列表框绑定到ObservableCollection,并在项目添加到集合时保持列表框更新。
我有一个名为CollectionOfBlogs的课程:
Public Class CollectionOfBlogs
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Public Sub New(ByVal name As String)
Me.FullName = name
End Sub
Private _FullName As String
Public Property FullName() As String
Get
Return _FullName
End Get
Set(ByVal value As String)
_FullName = value
NotifyPropertyChanged("FullName")
End Set
End Property
Public Sub NotifyPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
然后我在另一个类中设置了CollectionOfBlogs的ObservableCollection(上图)和一个用于向集合中添加项目的子项:
Public Class ITRSBlogs
Public blogNamesList As New ObservableCollection(Of CollectionOfBlogs)
Public Sub addBlog(ByVal FullName as String)
blogNamesList.Add(New CollectionOfBlogs(FullName))
End Sub
End Class
我将列表框从我的主页加载事件绑定到ITRSBlogs类(上面)中的blogNamesList集合:
Dim blogClass As New ITRSBlogs
Me.BloggingMenuListBox.ItemsSource = blogClass.blogNamesList
这是我的列表框的XAML。它受到代码隐藏的束缚,而不是XAML(我想应该提一下)。
<ListBox Name="BloggingMenuListBox"/>
在将集合绑定到列表框之前,我使用数据库中的项加载集合,它们显示在列表框中就好了。下面的这2个子实际上也在ITRSBlogs类中,我从我的页面加载事件中调用FillBlogLists。
Public Sub FillBlogLists()
Dim query = theContext.GetBlogsOrderedByNameQuery
theContext.Load(query, AddressOf OnBlogsLoaded, Nothing)
End Sub
Private Sub OnBlogsLoaded(ByVal lo As LoadOperation(Of Blog))
blogList.Clear()
For Each s In lo.AllEntities
blogList.Add(CType(s, Blog))
Next
For Each item In blogList
blogNamesList.Add(New CollectionOfBlogs(item.FullName))
Next
End Sub
除此之外,我在页面上有一个简单的文本框和按钮。当在文本框中输入名称并单击该按钮时,我会调用ITRSBlogs类中的addBlog(从文本框中传入名称)子例程(稍微备份页面)以将项目添加到集合中。
问题是,当我向集合添加项目时,列表框不会更新。我是Observable Collections的新手(以及许多其他东西:),所以也许我真的离开这里。谁能告诉我我做错了什么?
答案 0 :(得分:1)
您发布的代码对我来说很好。我复制了你的代码(在C#中,但我尽量保持它尽可能接近你的代码),它只是起作用。您未发布的唯一代码是Add
按钮的事件处理程序。您是否检查过它是否已被实际调用并且新项目已添加到集合中?
无论如何这里是我的代码,也许你可以发现它与你的版本之间的区别:
CollectionOfBlogs.cs:
public class CollectionOfBlogs : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string _FullName;
public string FullName
{
get
{
return this._FullName;
}
set
{
if (this._FullName != value)
{
this._FullName = value;
this.OnPropertyChanged("FullName");
}
}
}
public CollectionOfBlogs(string name)
{
this._FullName = name;
}
public virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public override string ToString()
{
return this._FullName;
}
}
ITRSBlogs.cs:
public class ITRSBlogs
{
public ObservableCollection<CollectionOfBlogs> blogNamesList = new ObservableCollection<CollectionOfBlogs>();
public void AddBlog(string fullName)
{
this.blogNamesList.Add(new CollectionOfBlogs(fullName));
}
public void FillBlogList()
{
this.blogNamesList.Clear();
this.AddBlog("First blog");
this.AddBlog("Another blog");
}
}
MainWindow.xaml:
<Window x:Class="ObservableRefreshDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<ListBox Name="BloggingMenuListBox"/>
<Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="154,276,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="12,276,0,0" Name="txtName" VerticalAlignment="Top" Width="120" />
</Grid>
</Window>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
private ITRSBlogs blogClass = new ITRSBlogs();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.blogClass.FillBlogList();
this.BloggingMenuListBox.ItemsSource = this.blogClass.blogNamesList;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.blogClass.AddBlog(this.txtName.Text);
}
}