ICommand in Silverlight 4


In Silverlight 4, ButtonBase (and all its derived versions) and HyperlinkButton now support Command and CommandParameter to tie ICommand implementations.  For example, in XAML I can tie two different buttons to the SaveCommand that I have on some object i’ve bound to my UI:

<Border BorderBrush="LightGray"
        BorderThickness="1">
  <StackPanel Orientation="Horizontal">
    <TextBlock>Tools:</TextBlock>
    <Button Command="{Binding SaveCommand}"
            CommandParameter="{Binding Person}"
            Content="Save" />
  </StackPanel>
</Border>
<StackPanel Grid.Row="1">
  <TextBlock FontWeight="Bold"
             Text="Person" />
  <TextBlock Text="Name" />
  <TextBox Text="{Binding Person.Name, Mode=TwoWay}" />
  <TextBlock Text="Occupation" />
  <TextBox Text="{Binding Person.Occupation, Mode=TwoWay}" />
  <Button Command="{Binding SaveCommand}"
          CommandParameter="{Binding Person}"
          Width="100"
          HorizontalAlignment="Right"
          Content="Save" />
</StackPanel>

By binding a SaveCommand to both of these buttons, whenever either of the buttons is pressed, the command will be executed.  But perhaps even more important is that as the command isn’t valid (perhaps when the Person doesn’t have changes), the buttons will be disabled.

For more on how to implement the command in C# goto http://wildermuth.com/2010/01/21/New_SL4_Feature_Commanding

One thought on “ICommand in Silverlight 4

  1. Pingback: Links (1/31/2010) « Steve Pietrek-Everything SharePoint/Silverlight

Leave a comment