Binding Classes
In Avalonia, you also can bind classes. Sometimes it could be useful to switch classes depending on some logic, and for those purposes, you can use Binding Classes API. There is the sample usage of Binding Classes. We have two different styles and we want to switch between them depending on MyProperty state.
1
<ListBox Items="{Binding MyItems}">
2
<ListBox.Styles>
3
<Style Selector="TextBlock.myClass">
4
<Setter Property="Background" Value="Red" />
5
</Style>
6
<Style Selector="TextBlock.myClass2">
7
<Setter Property="Background" Value="Green" />
8
</Style>
9
</ListBox.Styles>
10
<ListBox.ItemTemplate>
11
<DataTemplate>
12
<StackPanel>
13
<TextBlock
14
Classes.myClass="{Binding MyProperty}"
15
Classes.myClass2="{Binding !MyProperty}"
16
Text="{Binding Name}"/>
17
</StackPanel>
18
</DataTemplate>
19
</ListBox.ItemTemplate>
20
</ListBox>
Copied!
When you bind to classes Avalonia would expect bool value. This API was introduced in 0.10.1
Last modified 1mo ago
Export as PDF
Copy link