Replies: 2 comments
-
Hey did you manage to solve this ? |
Beta Was this translation helpful? Give feedback.
0 replies
-
In A's view model, you can subscribe to B's PropertyChanged event handler, e.g. internal partial class A : ObservableObject
{
public B B { get; } = new B();
[ObservableProperty]
private string aa;
public A()
{
B.PropertyChanged += (s, e) =>
{
switch (e.PropertyName)
{
case nameof(B.Bbb):
Aa = "Aa: " + value.Bbb;
break;
}
};
}
} Because Aa is a derivative of B.Bbb, I would implement Aa with a read-only getter, i.e. internal partial class A : ObservableObject
{
public B B { get; } = new B();
public string Aa => "Aa: " + B.Bbb;
public A()
{
B.PropertyChanged += (s, e) =>
{
switch (e.PropertyName)
{
case nameof(B.Bbb):
OnPropertyChanged(nameof(Aa));
break;
}
};
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I would like to utilize 'dot-notation' in my views to avoid dummy properties in the view models. However I have not been able to propagate UI changes of child properties to the view model. Is it even possible? Following is a simplified example of what I am trying to accomplish:
View:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="MauiApp1.Views.MyPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodels="clr-namespace:MauiApp1.ViewModels" Title="MyPage" x:DataType="viewmodels:MyViewModel"> <VerticalStackLayout> <Entry Text="{Binding A.B.Bb}" /> <Button IsEnabled="{Binding IsButtonEnabled}" Text="{Binding ButtonText}"/> <!-- These labels are just for debug purposes --> <Label Text="{Binding A.B.Bbb}" /> <Label Text="{Binding A.Aa}" /> </VerticalStackLayout> </ContentPage>
ViewModel:
`using CommunityToolkit.Mvvm.ComponentModel;
using MauiApp1.Models;
namespace MauiApp1.ViewModels;
internal partial class MyViewModel : ObservableObject
{
internal MyViewModel()
{
a = new A { B = new B() };
}
}
`
Model classes:
`using CommunityToolkit.Mvvm.ComponentModel;
namespace MauiApp1.Models;
internal partial class A : ObservableObject
{
[ObservableProperty]
private B b;
partial void OnBChanged(B value)
{
Aa = "Aa: " + value.Bbb;
}
}
using CommunityToolkit.Mvvm.ComponentModel;
namespace MauiApp1.Models;
internal partial class B : ObservableObject
{
[ObservableProperty]
private string bb;
partial void OnBbChanged(string value)
{
Bbb = "Bbb: " + value;
}
}
`
Label Bbb is updating just fine, but Aa and the button is not.
Also A.OnBChanged is not called - which is probably the core of my problem. Please help.
Beta Was this translation helpful? Give feedback.
All reactions