繁体   English   中英

与ObservableCollection数据绑定的列表框不会更新UI

[英]Listbox with databinding to ObservableCollection won't update UI

标题中已经说明了该问题。 请仔细查看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类的新手,但我很快就了解新事物。

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用新数据刷新View是有趣的。

更新:

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

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

...

但是正如您看到的那样,这有点困难,因为Notes应该是一个类,因此您可以从ObservableCollection继承:

public class Notes : ObservableCollection<Note>
{
}

暂无
暂无

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

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