簡體   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