microsoft / xaml-standard

XAML Standard : a set of principles that drive XAML dialect alignment
Other
805 stars 50 forks source link

Event Binding to Commands #160

Open MovGP0 opened 7 years ago

MovGP0 commented 7 years ago

it should be possible to bind Events to Commands in the ViewModel.

For controls that support command binding this is straight-forward:

<Button x:Name="SaveButton" 
   Command="{x:Bind SaveCommand}" CommandParameter="{x:Bind Name= ... , Path= ..., Mode=OneWay}" /> 

Otherwise it requires triggers:

<Button x:Name="SaveButton">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <command:EventToCommand 
                                Command="{x:Bind SaveCommand}" 
                                CommandParameter="{x:Bind Name= ... , Path= ..., ... }" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListView>

There might also be an additional binding to make this simpler:

<Button x:Name="SaveButton" Click="{eb:Bind Command={x:Bind SaveCommand}, CommandParameter={x:Bind Name= ... , Path= ... , ...} }" />

Note: if you aren't familiar with compiled bindings (x:Bind instead of Binding), see Compiled Binding – What is it?