How to bind a command to an event? #6171
-
I want to bind a reactive command to an event so I tried using the attached properties. I copied this code from the official Avalonia documentation about creating and binding attached properties but I get this error:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
You can use Behaviors
Example: <UserControl x:Class="BehaviorsTestApplication.Views.Pages.InvokeCommandActionView"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="using:Avalonia.Xaml.Interactivity"
xmlns:ia="using:Avalonia.Xaml.Interactions.Core"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="600" d:DesignHeight="450">
<Grid RowDefinitions="*,Auto" ColumnDefinitions="30*,5,30*,5,30*">
<Canvas Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="5" Margin="5" Background="{DynamicResource GrayBrush}">
<Ellipse Canvas.Left="{Binding Position, Mode=TwoWay}" Canvas.Top="200" Fill="{DynamicResource RedBrush}" Stroke="{DynamicResource YellowBrush}" StrokeThickness="5" Height="100" Width="100"/>
</Canvas>
<Button x:Name="MoveLeftButton" Content="Move Left" Grid.Row="1" Grid.Column="0" Margin="5,0,0,5">
<i:Interaction.Behaviors>
<ia:EventTriggerBehavior EventName="Click" SourceObject="{Binding #MoveLeftButton}">
<ia:InvokeCommandAction Command="{Binding MoveLeftCommand}"/>
</ia:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Button>
<Button x:Name="ResetMoveButton" Content="Reset Move" Grid.Row="1" Grid.Column="2" Margin="0,0,0,5">
<i:Interaction.Behaviors>
<ia:EventTriggerBehavior EventName="Click" SourceObject="{Binding #ResetMoveButton}">
<ia:InvokeCommandAction Command="{Binding ResetMoveCommand}"/>
</ia:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Button>
<Button x:Name="MoveRightButton" Content="Move Right" Grid.Row="1" Grid.Column="4" Margin="0,0,5,5">
<i:Interaction.Behaviors>
<ia:EventTriggerBehavior EventName="Click" SourceObject="{Binding #MoveRightButton}">
<ia:InvokeCommandAction Command="{Binding MoveRightCommand}"/>
</ia:EventTriggerBehavior>
</i:Interaction.Behaviors>
</Button>
</Grid>
</UserControl> Sample app: https://github.com/wieslawsoltes/AvaloniaBehaviors/tree/master/samples/BehaviorsTestApplication |
Beta Was this translation helpful? Give feedback.
-
@NekrasovaLera hi, I updated docs page. There was out of dated example. |
Beta Was this translation helpful? Give feedback.
You can use Behaviors
Example: