Tuesday, May 17, 2011

Triggering Multiple Commands on a Single Button Click in Silverlight XAML

If you're using RIA services and have ever drag-and-dropped a datasource onto a user control, you've likely seem the generated Load button that fires a command to load data:

<Button Content="Load" Margin="3" Name="myDomainDataSourceLoadButton" Command="{Binding Path=LoadCommand, ElementName=myDomainDataSource}" />

If your control has multiple DomainDataSources and you'd like to trigger them all by a single button click, you can change your markup:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

<Button Content="Load" Margin="3" Name="myDomainDataSourceLoadButton">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <i:InvokeCommandAction Command="{Binding Path=LoadCommand, ElementName=myDomainDataSource}"/>
            <i:InvokeCommandAction Command="{Binding Path=LoadCommand, ElementName=myOtherDomainDataSource}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

I left out the markup for the DomainDataSource and its query parameters, etc, but this should get the point across.

No comments: