ObservableCollection绑定到列表框ui没有更新

本文关键字:更新 ui 绑定 列表 ObservableCollection | 更新日期: 2023-09-27 18:09:04

我知道我应该使用MVVM模式,但我正试图一步一步地接近它。这是我的Listbox:

<ListBox x:Name="BoardList" ItemsSource="{Binding notes}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                            <TextBox IsReadOnly="True" ScrollViewer.VerticalScrollBarVisibility="Visible" Text="{Binding text}" TextWrapping="Wrap" Foreground="DarkBlue"></TextBox>
                            <AppBarButton Visibility="{Binding visibility}" Icon="Globe" Click="OpenInBrowser" x:Name="Link"></AppBarButton>
                            <AppBarButton Icon="Copy" Click="Copy"></AppBarButton>
                            <AppBarButton Icon="Delete" Click="Delete"></AppBarButton>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

在mainpage . example .cs中我声明如下:

    ObservableCollection<BoardNote> notes   = new ObservableCollection<BoardNote>();

所以如果我理解对了,我不需要关心"INotifyCollectionChanged"的东西,因为我正在使用一个可观察的集合?比如我有一个这样的文本框:

<Textbox x:Name="UserInputNote" Placeholdertext="Type in a text for your note"></Textbox>

和一个按钮添加新的笔记到ObservableCollection和点击事件就像这样:

notes.Add(new BoardNote(UserInputNote.Text));

所以现在UI应该在每次用户单击按钮保存新注释时更新。但是什么也没发生。我做错了什么?

如果你需要它,这里是BoardNote类:

   class BoardNote
{
    public string text
    {
        get; set;
    }
        public BoardNote(string text)
    {
        this.text = text;
    }
    public Visibility visibility
    {
        get
        {
            if (text.StartsWith("http"))
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }
    }
}

ObservableCollection绑定到列表框ui没有更新

您需要实现INotifyPropertyChanged。这里有一种方法。

创建NotificationObject类

 public class NotificationObject : INotifyPropertyChanged
 {
    protected void RaisePropertyChanged<T>(Expression<Func<T>> action)
    {
        var propertyName = GetPropertyName(action);
        RaisePropertyChanged(propertyName);
    }
    private static string GetPropertyName<T>(Expression<Func<T>> action)
    {
        var expression = (MemberExpression)action.Body;
        var propertyName = expression.Member.Name;
        return propertyName;
    }
    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

那么你的BoardNote类将以这种方式继承它:

class BoardNote : NotificationObject
{
private string _text
public string Text
{
    get {return _text;}
    set 
    {
     if(_text == value) return;
     _text = value;
     RaisePropertyChanged(() => Text);
    }
}
    public BoardNote(string text)
{
    this.text = text;
}
public Visibility visibility
{
    get
    {
        if (text.StartsWith("http"))
            return Visibility.Visible;
        else
            return Visibility.Collapsed;
    }
}
}