Avalonia: Add a way of setting style classes using bindings

Created on 4 Apr 2019  路  5Comments  路  Source: AvaloniaUI/Avalonia

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:

Simple binding

<Button Classes="{Binding ButtonClasses}">

Pros:

  • Simple

Cons:

  • Will need to either specify actual classes in view model or supply a converter
  • Would not be able to specify classes in XAML as well as get them from the binding

Class Setters

<Button>
  <Button.Classes>
    <ClassSetter Name="foo"/>
    <ClassSetter Name="animated" Trigger="{Binding IsButtonAnimating}"/>
  </Button.Casses>
</Button>

Access DataContext from Selectors

An alternative to binding classes would be to allow _selectors_ to select into the DataContext.

Pros:

  • Probably pretty flexible

Cons:

  • Syntax?
enhancement

Most helpful comment

Just created behavior for this.
Hope it will be useful for somebody
https://gist.github.com/maxkatz6/2c765560767f20cf0483be8fac29ff22

All 5 comments

Yes please, that's exactly what's missing to replace DataTriggers

Pros

  • Simple and powerfull
  • Can style many properties (as selectors do)

Cons:

  • Just coding it? Assuming providing a few comparison operators: =, >, <, IN(), ! is enough?

Syntax:

Control(MyDataContextProperty=Value)

1362

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JonathaN7Shepard picture JonathaN7Shepard  路  4Comments

kekekeks picture kekekeks  路  4Comments

MiKaMaru picture MiKaMaru  路  4Comments

MarchingCube picture MarchingCube  路  4Comments

GitHubington picture GitHubington  路  3Comments