繁体   English   中英

绑定到ObservableCollection的列表框(在VB.NET和Silverlight中)

[英]Listbox bound to ObservableCollection (in VB.NET and Silverlight)

抱歉,如果这是一个重复的问题。 关于这个话题有很多问题,但是我似乎无法弄清楚。 我正在尝试将列表框绑定到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"/>

在将集合绑定到列表框之前,我用数据库中的项目加载了集合,它们很好地显示在列表框中。 下面的这两个子实际上也位于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的新手(以及许多其他东西:),所以也许我真的不在这里。 谁能告诉我我在做什么错?

您发布的代码对我来说很好。 我复制了您的代码(在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);
    }
}

暂无
暂无

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

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