'Binding to a static property doesn't update in Xamarin Forms
I am learning to use MVVM in Xamarin Forms. I am stuck with a Binding issue. I have been trying different solutions on the internet for the past few hours. I am trying to bind a static property called count
to the Title
of a Content Page
. The Title
shows "0 PlayLists" initially. But it doesn't get updated as I add PlayLists.
Model Class
namespace MVVMDemo.Models
{
public class PlayList : INotifyPropertyChanged
{
public static event PropertyChangedEventHandler StaticPropertyChanged;
public string Title { get; set; }
private static int count { get; set; } = 0;
public static int Count
{
get { return count; }
set
{
if (count == value)
return;
count = value;
OnStaticPropertyChanged();
}
}
private static void OnStaticPropertyChanged([CallerMemberName] string propertyName = null)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
}
}
ViewModel Page
public void AddPlayList()
{
playList.Add(new PlayList
{
Title = string.Format("PlayList {0}", PlayList.Count + 1)
});
PlayList.Count++;
}
XAML Page
<ContentPage
xmlns:local="clr-namespace:MVVMDemo.Models"
Title="{Binding Source={x:Static local:PlayList.Count}, Mode=TwoWay, StringFormat='{0} Playlists'}"
>
The count property is getting updated everytime AddPlayList()
is run. The XAML file is not updating as the count
changed. I have tried to use Path
instead of Source
. But it doesn't show anything. Please suggest me the correct syntax to use Path
if that is the issue!
Solution 1:[1]
To implement data changed notify, you could implement INotifyPropertyChanged
Interface in Xamarin.forms.
But, the static classes could not implement interface. That's why your solution would not work.
You could try the code below with INotifyPropertyChanged
:
public class PlayList : INotifyPropertyChanged
{
public string Title { get; set; }
private int count { get; set; } = 0;
public int Count
{
get { return count; }
set
{
if (count == value)
return;
count = value;
OnPropertyChanged("Count");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
}
With your code to binding the Count for Title, it would always be 0.
Model: private static int count { get; set; } = 0;
Xaml Binding: Title="{Binding Source={x:Static local:PlayList.Count}, Mode=TwoWay, StringFormat='{0} Playlists'}"
You could use the code below to get the count according to the AddPlayList() method
.
ViewModel:
public class PlayListViewModel
{
public static List<PlayList> playList { get; set; }= new List<PlayList>();
public static int Count { get; set; }
static PlayListViewModel()
{
AddPlayList();
AddPlayList();
AddPlayList();
}
public static void AddPlayList()
{
playList.Add(new PlayList
{
Title = string.Format("PlayList {0}", PlayList.Count + 1)
});
Count++;
}
}
Xaml binding:
Title="{Binding Source={x:Static local:PlayListViewModel.Count}, Mode=TwoWay, StringFormat='{0} Playlists'}"
For binding of static property, you could refer to the MS docs. https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-bindings-to-mvvm#implementing-a-navigation-menu
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Wendy Zang - MSFT |