Bind to support viewmodel with property within a model #203
-
Hi, I am not sure if this is a bug or if what I need a feature. I tried to enter it as a bug but had a change of heart. When this code is used the binding is not executed. .Bind(Label.TextProperty, static v => v.CoreSalesVsCoreBudget, source: _viewModel.Sales,
convert: (double d, string s) => d > 0 ? "Sales Ahead" : "Sales Behind") When the value of "CoreSalesVsCoreBudget" changes or the "Sales" changes the converter should update the Label Text. I have tried to do this multiple ways and none have worked so far. However in XAML this works just fine. I am trying to duplicate this Converter. This XAML Markup works just fine. Notice the binding "Sales.CoreSalesVsCoreBudget". <Label.Triggers>
<DataTrigger
Binding="{Binding Sales.CoreSalesVsCoreBudget, Converter={StaticResource IsNegativeToBoolConverter}}"
TargetType="Label"
Value="True"
>
<Setter Property="Text" Value=" Behind of Core Sales " />
</DataTrigger>
<DataTrigger
Binding="{Binding Sales.CoreSalesVsCoreBudget, Converter={StaticResource IsNegativeToBoolConverter}}"
TargetType="Label"
Value="False"
>
<Setter Property="Text" Value=" Ahead of Core Sales " />
</DataTrigger>
</Label.Triggers> Also, when I remove the "Sales" Model and code define use the double number direct "CoreSalesVsBudget" as follows: .Bind(Label.TextProperty, static v => v.CoreSalesVsCoreBudget, source: _viewModel,
convert: (double d, string s) => d > 0 ? "Sales Ahead" : "Sales Behind") This works. How to recreate this issue.
public class SalesModel
{
public double CoreSales { get; set; }
}
[ObservableProperty]
private SalesModel _sales;
.Bind(Label.TextProperty, static v => v.CoreSales, source: _viewModel.Sales,
convert: (double d, string s) => d > 0 ? "Sales Ahead" : "Sales Behind") CommunityToolkit.Maui" Version="5.0.0" /> |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I know @brminnick explained to me how to do this and I thought I added to the docs but I can't find it anywhere. Hopefully he can point us in the right direction and I will get it added to the docs |
Beta Was this translation helpful? Give feedback.
-
You're doing a Nested Binding which requires a bit more configuration. This is because your 👇 Here's a few ways how you can solve your problem 1. Create a Nested BindingThis is technically the correct way to do nested bindings. However, it is also the most complex way to create a binding. Here's how to configure .Bind(Label.TextProperty,
static vm => vm.Sales.CoreSales,
new (Func<ViewModel, object?>, string)[] // Replace `ViewModel` with the `Type` of your ViewModel
{
(vm => vm, nameof(ViewModel.Sales)), // Replace `ViewModel` with the `Type` of your ViewModel
(vm => vm.Sales, nameof(ViewModel.Sales)), // Replace `ViewModel` with the `Type` of your ViewModel
(vm => vm.Sales.CoreSales, nameof(ViewModel.Sales.CoreSales)), // Replace `ViewModel` with the `Type` of your ViewModel
},
convert: (double d, string s) => d > 0 ? "Sales Ahead" : "Sales Behind")) Example from
|
Beta Was this translation helpful? Give feedback.
-
Closed and answered |
Beta Was this translation helpful? Give feedback.
You're doing a Nested Binding which requires a bit more configuration. This is because your
ObservableProperty
is theSales
property in the ViewModel, however you want the value of itsCoreSales
Property nested insideSales.CoreSales
. And .NET MAUI's binding engine needs to know about every property + nested property along the way.👇 Here's a few ways how you can solve your problem
1. Create a Nested Binding
This is technically the correct way to do nested bindings. However, it is also the most complex way to create a binding.
Here's how to configure
.Bind()
to bind toCoreSales
while telling .NET MAUI binding engine to monitor it along with its parent classSales
.