C#XAML:UI后不更新改变的ObservableCollection的一个实例 [英] C# XAML : UI not update after change an instance of ObservableCollection

查看:145
本文介绍了C#XAML:UI后不更新改变的ObservableCollection的一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的C#和尝试开发Windows 10 UWP应用作为一种业余爱好的项目。

I am fairly new to C# and try to develop Windows 10 UWP app as a hobby project.

MainBrowser.XAML部分

Part of MainBrowser.XAML

            <GridView x:Name="browserPane" ItemsSource="{x:Bind fileInCurrentFolderList,Mode=OneWay}" SelectionChanged="browserPane_SelectionChanged" Margin="0,48,0,0">
                <GridView.ItemTemplate>
                    <DataTemplate x:DataType="classes:Item">
                        <StackPanel Margin="10">
                            <Image Height="268" Width="200" Source="{x:Bind Thumbnail}" x:Phase="1" Stretch="UniformToFill"></Image>
                            <TextBlock Width="200" Text="{x:Bind Caption}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" MaxLines="2"/>
                        </StackPanel>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>

这网格视图是绑定到

public sealed partial class MainBrowser : Page
{
...
private ObservableCollection<Item> fileInCurrentFolderListUI = new ObservableCollection<Item>();
...
}

有是按钮的左边的列表应用程序的一面。每个按钮将调用,将返回的方法的的ObservableCollection<项目方式>

There is a list of button on the left side of the app. Each button will call a method that will return a ObservableCollection<Item> back.

问题是我需要做这样的事情。

The problem is I need to do something like

foreach (Item file in ReturnObservableCollection)
    {
    fileInCurrentFolderList.Add(item);
    }



相反,这个

Instead of this

fileInCurrentFolderList = ReturnObservableCollection;

要能够UI触发更新。
我怎样才能改变呢?

To able to trigger update in UI. How can I change this?

推荐答案

这是怎么回事的是,的ObservableCollection 的报告。一种解决方案是使用 INotifyPropertyChanged的接口的视图模型并报告更改属性。

What's happening is that ObservableCollection is reporting when items are added or removed from it, but if the collection itself changes (i.e. a new instance is instantiated) there's nothing to report the change. One solution is to use the INotifyPropertyChanged interface in your ViewModel and report changes to the properties.

public sealed partial class MainBrowser : Page, INotifyPropertyChanged
{
    // Backing field.
    private ObservableCollection<Item> fileInCurrentFolderListUI;
    // Property.
    public ObservableCollection<Item> FileInCurrentFolderListUI
    {
        get { return fileInCurrentFolderListUI; }
        set
        {
            if (value != fileInCurrentFolderListUI)
            {
                fileInCurrentFolderListUI = value;
                // Notify of the change.
                NotifyPropertyChanged();
            }
        }
    }

    // PropertyChanged event.
    public event PropertyChangedEventHandler PropertyChanged;

    // PropertyChanged event triggering method.
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

您可以初始化支持字段在声明中,你之前所做的或只是初始化在构造函数的性质。只要确保你绑定的财产,而不是支持字段。此外,如果你要分配一个新的对象,请确保您这样做是为了财产,使更改可能会播出。基本上,不支持字段进行交互,只是做通过属性的一切。

You can initialize the backing field in the declaration as you've done before or just initialize the property in the constructor. Just make sure that you bind to the property and not the backing field. Additionally, if you are going to assign a new object, make sure you do it to the property, so that the change may be broadcasted. Basically, don't interact with the backing field, just do everything through the property.

这篇关于C#XAML:UI后不更新改变的ObservableCollection的一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆