Reputation: 5462
Binding to observable property does not work when I try to create my own custom behavior. Neither it does in any of community mvvm toolkit platform behaviors:
Take for example StatusBarBehavior
, write something like
<ContentPage.Behaviors>
<toolkit:StatusBarBehavior StatusBarColor="{Binding StatusBarColorProp}" StatusBarStyle="LightContent" />
</ContentPage.Behaviors>
create the property in your view model
[ObservableProperty]
private Color _statusBarColorProp;
you'll see status bar color does not change with StatusBarBehavior
property change in runtime. Same for all the rest behaviors. It works fine for non-bindings setters like StatusBarColor="Red"
.
I wonder if it's a feature or a bug, or I'm missing something.
UPDATE the issue is reported https://github.com/dotnet/maui/issues/11729
Upvotes: 2
Views: 680
Reputation: 1
Faced a similar binding problem. Behavior class:
public class EntryNumericBehavior : Behavior<Entry>
{
public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(int), typeof(EntryNumericBehavior));
public int MinValue
{
get => (int)GetValue(MinValueProperty);
set { SetValue(MinValueProperty, value); }
}
}
Binding in XAML. MinTemperature - source property:
<Entry>
<Entry.Behaviors>
<ViewModels:EntryNumericBehavior MinValue="{Binding MinTemperature}"/>
</Entry.Behaviors>
</Entry>
During experiments, it turned out that if MinTemperature is bound to Label, you can bind to it in XAML (crutch):
<Label Text="{Binding MinTemperature}" x:Name="MinLabel"/>
<Entry>
<Entry.Behaviors>
<ViewModels:EntryNumericBehavior MinValue="{Binding Source={x:Reference MinLabel}, Path=Text}"/>
</Entry.Behaviors>
</Entry>
I realize it's not a great solution, but it's a temporary one.
Upvotes: 0
Reputation: 21321
Please file a bug issue at github maui issues.
In the meantime, try this gross hack in one of your properties:
static public BindableProperty MyValueProperty = BindableProperty.Create(...,
propertyChanged: (bindable, oldValue, newValue) =>
{
var it = (MyClass)bindable;
// If it is changing, force explicit OnPropertyChanged. This is usually redundant,
// but might help a binding "cascade" to dependencies.
if (!(MyType)newValue.Equals((MyType)oldvalue))
it.OnPropertyChanged(nameof(MyValue));
});
public MyType MyValue
{
get => (MyType)GetValue(MyValueProperty);
set => SetValue(MyValueProperty, value);
}
Upvotes: 0
Reputation: 10148
I can replicate your issue. And it turns out that we can only change the color of the StatusBarColor
either in code behind like below or non-bindings setters like StatusBarColor="Red"
as you mentioned.
<ContentPage.Behaviors>
<toolkit:StatusBarBehavior x:Name="statusBar" ></toolkit:StatusBarBehavior>
</ContentPage.Behaviors>
private void OnCounterClicked(object sender, EventArgs e)
{
statusBar.StatusBarColor = Colors.Red;
}
It fails to change the color of StatusBarColor
when binding to an observable property. This could be a potential issue and I would suggest that you can raise a Bug Report in Github.
Upvotes: 3