Vincent Leung .NET Tech Clips

The latest tech clips from the .NET community

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

About these ads

January 27, 2010 - Posted by | Silverlight

1 Comment »

  1. [...] ICommand in Silverlight 4 [...]

    Pingback by Links (1/31/2010) « Steve Pietrek-Everything SharePoint/Silverlight | January 31, 2010 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: