justanotherxl
justanotherxl

Reputation: 319

How to update bindings inside of a gridview?

I'll use the solution files from here as an example: https://github.com/Windows-Readiness/AbsoluteBeginnersWin10/tree/master/UWP-040/UWP-040 (If the creator has a problem with me linking these for the example, I'll make my own and upload them). Currently, the gridview is bound to a List of a class book. One of the properties that the itemtemplate for the gridview binds to is called Title. However, if I update Title, and call Bindings.Update(), it will not update it. To illustrate this, I added a button onto the page, and made its actions this:

Books[0].Title = "hello";
Bindings.Update();

Now when I press the button, what I want to have happen is for the first item on the gridview to change it's Title to "hello". However, nothing happens. How do I achieve this behavior? The solution needs to work in C++/CX too, because that's where I'll be using it.

Upvotes: 2

Views: 517

Answers (1)

Bart
Bart

Reputation: 10015

The implementation of Book in your sample code is

public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public string Author { get; set; }
    public string CoverImage { get; set; }
}

If you want to update properties in code, you have to implement INotifyPropertyChanged. Any property changes will then automatically propagate to your UI. Several MVVM frameworks provide a base implementation to make your work easier.

EDIT: as @justanotherxl noticed correctly: x:Bind defaults to OneTime, for updates this has to changed to OneWay (which is the default of regular bindings so those work correctly out of the box).

Upvotes: 1

Related Questions