I guess this is how you propertychange a dependencyproperty

This commit is contained in:
Mike Griese
2024-12-12 14:12:21 -06:00
parent 4f8b905369
commit bae9c0cf3b

View File

@@ -27,24 +27,12 @@ public sealed partial class ListPage : Page,
public ListViewModel? ViewModel
{
get => (ListViewModel?)GetValue(ViewModelProperty);
set
{
if (ViewModel != null)
{
ViewModel.PropertyChanged -= ViewModel_PropertyChanged;
}
SetValue(ViewModelProperty, value);
if (value != null)
{
value.PropertyChanged += ViewModel_PropertyChanged;
}
}
set => SetValue(ViewModelProperty, value);
}
// Using a DependencyProperty as the backing store for ViewModel. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ViewModelProperty =
DependencyProperty.Register(nameof(ViewModel), typeof(ListViewModel), typeof(ListPage), new PropertyMetadata(null));
DependencyProperty.Register(nameof(ViewModel), typeof(ListViewModel), typeof(ListPage), new PropertyMetadata(null, OnViewModelChanged));
public ViewModelLoadedState LoadedState
{
@@ -179,6 +167,22 @@ public sealed partial class ListPage : Page,
}
}
private static void OnViewModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is ListPage @this)
{
if (e.OldValue is ListViewModel old)
{
old.PropertyChanged -= @this.ViewModel_PropertyChanged;
}
if (e.NewValue is ListViewModel page)
{
page.PropertyChanged += @this.ViewModel_PropertyChanged;
}
}
}
private void ViewModel_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
var prop = e.PropertyName;