Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# XAML : UI not update after change an instance of ObservableCollection

Tags:

c#

xaml

uwp

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

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>

This grid view is bind to

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

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;

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

like image 821
Suttisak Avatar asked Sep 13 '15 04:09

Suttisak


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

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.

like image 181
B.K. Avatar answered Oct 12 '22 07:10

B.K.