Avalonia.Xaml.Behaviors
Avalonia.Xaml.Behaviors copied to clipboard
Can't use static object binding with DataTriggerBehavior
If a DataTriggerBehavior is bound with {Binding ..., Source={x:Static ...}}
, the OnValueChanged call happens before AttachedValue is set, so it fails. I tried saving the event and calling it again in OnAttached()
, but at this point the Value property on any ChangePropertyActions has not been set, so it still fails.
Example code (in BehaviorsTestApplication): DataTriggerBehaviorControl.xaml
<Grid RowDefinitions="5*,*">
<Rectangle Name="DataTriggerRectangle" Grid.Row="0" Margin="5" Stroke="{DynamicResource GrayBrush}" StrokeThickness="5">
<i:Interaction.Behaviors>
<ia:DataTriggerBehavior Binding="{Binding ., Source={x:Static vm:MainWindowViewModel.TestInt}}"
ComparisonCondition="GreaterThan" Value="50">
<ia:ChangePropertyAction PropertyName="Fill" TargetObject="{Binding #DataTriggerRectangle}" Value="{DynamicResource YellowBrush}" />
</ia:DataTriggerBehavior>
<ia:DataTriggerBehavior Binding="{Binding ., Source={x:Static vm:MainWindowViewModel.TestInt}}" ComparisonCondition="LessThanOrEqual" Value="50">
<ia:ChangePropertyAction PropertyName="Fill" TargetObject="{Binding #DataTriggerRectangle}" Value="{DynamicResource BlueBrush}" />
</ia:DataTriggerBehavior>
</i:Interaction.Behaviors>
</Rectangle>
<StackPanel Grid.Row="1" Margin="5,0,5,5" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<TextBlock Width="50" VerticalAlignment="Center" Foreground="{DynamicResource GrayBrush}" Text="{Binding ., Source={x:Static vm:MainWindowViewModel.TestInt}}" />
</StackPanel>
</Grid>
MainWindowViewModel.cs
public static int TestInt = 51;
Result:
The binding correctly updates the TextBox, but does not trigger the DataTriggerBehavior.