I have a listview which consumes custom view cell.
However View is not getting updated in case of property change event is triggered .
I can see that View Model is getting updated values in case event is triggered but my View is not getting updated.
CustomViewCell.xaml:
<Label HorizontalTextAlignment="Start"
VerticalTextAlignment="End"
FontSize="16"
FontAttributes="Bold"
LineBreakMode="WordWrap"
Grid.Column="0"
Grid.Row="0"
Text="{Binding TextMain}"
x:Name="MainTitleLabel"/>
<Label HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
FontSize="14"
Grid.Column="0"
Grid.Row="1"
Text="{Binding TextDetail}"
x:Name="TextDetailLabel"/>
<Image VerticalOptions="Center"
HorizontalOptions="EndAndExpand"
HeightRequest="24"
WidthRequest="24"
IsOpaque="True"
Grid.Column="2"
Grid.Row="0"
Grid.RowSpan="2"
Source="{Binding ImageSource}}"
x:Name="ImageSourceLabel"/>
CustomViewCell.xaml.cs:
public CustomViewCell()
{
InitializeComponent();
}
public static BindableProperty MainTitleProperty = BindableProperty.Create(nameof(MainTitle), typeof(string), typeof(CustomViewCell));
public string MainTitle
{
get => (string)GetValue(MainTitleProperty);
set => SetValue(MainTitleProperty, value);
}
public static BindableProperty TitleDetailProperty = BindableProperty.Create(nameof(TitleDetail), typeof(string), typeof(CustomViewCell));
public string TitleDetail
{
get => (string)GetValue(TitleDetailProperty);
set => SetValue(TitleDetailProperty, value);
}
public static BindableProperty ConnectionImageSourceProperty = BindableProperty.Create(nameof(ConnectionImageSource), typeof(Image), typeof(CustomViewCell));
public Image ConnectionImageSource
{
get => (Image)GetValue(ConnectionImageSourceProperty);
set => SetValue(ConnectionImageSourceProperty, value);
}
protected override void OnBindingContextChanged()
{
MainTitleLabel.Text = MainTitle;
DetailLabel.Text = TitleDetail;
ImageSourceLabel.Source = ConnectionImageSource.Source;
base.OnBindingContextChanged();
}
CustomViewCellViewModel .cs:
public class CustomViewCellViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
private string _mainTitleText = null;
public string MainTitleText
{
get { return _mainTitleText; }
set
{
if (_mainTitleText == null || !_mainTitleText.Equals(value))
{
_mainTitleText = value;
OnPropertyChanged("MainTitleText");
}
}
}
private string _titleDetailText = null;
public string TitleDetailText
{
get { return _titleDetailText; }
set
{
if (_titleDetailText == null || !_titleDetailText.Equals(value))
{
_titleDetailText = value;
OnPropertyChanged("TitleDetailText");
}
}
}
private string _imageSource = null;
public string ImageSource
{
get { return _imageSource; }
set
{
if (_imageSource == null || !_imageSource.Equals(value))
{
_imageSource = value;
OnPropertyChanged("ImageSource");
}
}
}