We really need a way to be able to apply to apply style classes to controls based on values from view models. This would be needed to e.g. trigger animations from the view model.
AvaloniaBehaviors has a behavior for this, but I think we should really have something in-box.
A few ideas:
<Button Classes="{Binding ButtonClasses}">
Pros:
Cons:
<Button>
<Button.Classes>
<ClassSetter Name="foo"/>
<ClassSetter Name="animated" Trigger="{Binding IsButtonAnimating}"/>
</Button.Casses>
</Button>
An alternative to binding classes would be to allow _selectors_ to select into the DataContext.
Pros:
Cons:
Yes please, that's exactly what's missing to replace DataTriggers
Pros
Cons:
Syntax:
Control(MyDataContextProperty=Value)
Is there any workarounds now? Since a ViewModel cannot access Controls to call .Classes.Add/Remove(), it seems to be difficult to change controls' class when members in ViewModel are updated.
You could write a behavior that is adding removing classes on some condition
It should be possible to solve problem from another issue with this feature https://github.com/AvaloniaUI/Avalonia/issues/4184
<Button>
<Button.Classes>
<!-- Property is read from DataContext, Value could be Binding also -->
<ClassSetter Name="PropEqualToValue" Property="{Binding Property}" Value="Value" />
</Button.Casses>
</Button>
...
<Style Selector="Button.PropEqualToValue" />
Or with boolean operators https://github.com/AvaloniaUI/Avalonia/issues/4122
<Button>
<Button.Classes>
<ClassSetter Name="PropEqualToValue" Trigger="{Binding Property == Value}" />
</Button.Casses>
</Button>
Or with functions in binding https://github.com/AvaloniaUI/Avalonia/issues/2493
<Button>
<Button.Classes>
<ClassSetter Name="PropEqualToValue" Trigger="{Binding Property.Equals(Value)}" />
</Button.Casses>
</Button>
Just created behavior for this.
Hope it will be useful for somebody
https://gist.github.com/maxkatz6/2c765560767f20cf0483be8fac29ff22
Most helpful comment
Just created behavior for this.
Hope it will be useful for somebody
https://gist.github.com/maxkatz6/2c765560767f20cf0483be8fac29ff22