Avalonia.Xaml.Behaviors icon indicating copy to clipboard operation
Avalonia.Xaml.Behaviors copied to clipboard

Can't use static object binding with DataTriggerBehavior

Open ThereGoesMySanity opened this issue 4 years ago • 0 comments

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: image

The binding correctly updates the TextBox, but does not trigger the DataTriggerBehavior.

ThereGoesMySanity avatar Oct 10 '20 06:10 ThereGoesMySanity