与ObservableCollection绑定的Listbox不能更新UI

本文关键字:不能 更新 UI Listbox ObservableCollection 绑定 | 更新日期: 2023-09-27 18:09:15

标题中已经说明了这个问题。请仔细看看XAML和代码,因为我是c#的业余爱好者(只有基本的知识),对数据绑定几乎是完全陌生的。这里是我的XAML:

<ListBox x:Name="BoardList" ItemsSource="{Binding notes, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" >
            <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.xaml.cs文件中创建一个ObservableCollection:

 public sealed partial class MainPage : Page
{
    ObservableCollection<BoardNote> notes = new ObservableCollection<BoardNote>();
    public MainPage()    //empty for post
    {
        this.InitializeComponent();
    }}

BoardNote:

 class BoardNote : NotificationObject
{
    private string _text { get; set; }
    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;
        }
    }
}

Notification类:

 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,例如在一个按钮上,Listbox不会添加一个新项目。我不知道该怎么做,我是绑定和INotifyPropertyChanged类的新手,但我理解新事物很快。

与ObservableCollection绑定的Listbox不能更新UI

1。—DataContext是一个类的实例,它包含了你需要的数据。您可以这样设置数据上下文:

    <Page xmlns:local...>
    <Page.DataContext>
      <local:ViewModelClass/>
    </Page.DataContext>    

(你可以通过多种方式做到这一点,作为资源和许多其他东西)

2。-类ViewModelClass应该包含一个名为Notes的属性,并且必须是一个属性。

public  ObservableCollection<BoardNote> Notes {get;} = new ObservableCollection<BoardNote>();

3。—如果需要更新数据,不需要重新实例Notes,只需清除它并读取条目。

4。-实现INotifyPropertyChanged使UI用新数据刷新视图是很有趣的。

更新:

如果您将数据上下文设置为Notes的实例:

<ListBox x:Name="BoardList" ItemsSource="{Binding}">
<ListBox.DataContext>
  <local:Notes/>
 </ListBox.DataContext/>

但是正如你所看到的,这有点困难,因为Notes应该是一个类,所以你可以继承ObservableCollection:

public class Notes : ObservableCollection<Note>
{
}