CheckBox Command Behaviors for Silverlight MVVM Pattern

Posted by Blake Blackwell on Stack Overflow See other posts from Stack Overflow or by Blake Blackwell
Published on 2010-04-27T20:40:28Z Indexed on 2010/04/27 20:43 UTC
Read the original article Hit count: 1117

Filed under:
|
|

I am trying to detect when an item is checked, and which item is checked in a ListBox using Silverlight 4 and the Prism framework. I found this example on creating behaviors, and tried to follow it but nothing is happening in the debugger. I have three questions:

  1. Why isn't my command executing?
  2. How do I determine which item was checked (i.e. pass a command parameter)?
  3. How do I debug this? (i.e. where can I put break points to begin stepping into this)

Here is my code:

View:

        <ListBox x:Name="MyListBox" ItemsSource="{Binding PanelItems, Mode=TwoWay}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding Enabled}" my:Checked.Command="{Binding Check}"  />
                        <TextBlock x:Name="DisplayName" Text="{Binding DisplayName}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

ViewModel:

public MainPageViewModel()
{
    _panelItems.Add( new PanelItem
    {
        Enabled = true,
        DisplayName = "Test1"
    } );

    Check = new DelegateCommand<object>( itemChecked );
}

public void itemChecked( object o )
{
//do some stuff
}

public DelegateCommand<object> Check { get; set; }

Behavior Class

public class CheckedBehavior : CommandBehaviorBase<CheckBox>
    {
        public CheckedBehavior( CheckBox element )
            : base( element )
        {
            element.Checked +=new RoutedEventHandler(element_Checked);
        }

        void element_Checked( object sender, RoutedEventArgs e )
        {
            base.ExecuteCommand();
        }               
    }

Command Class

public static class Checked
{
    public static ICommand GetCommand( DependencyObject obj )
    {
        return (ICommand) obj.GetValue( CommandProperty );
    }

    public static void SetCommand( DependencyObject obj, ICommand value )
    {
        obj.SetValue( CommandProperty, value );
    }

    public static readonly DependencyProperty CommandProperty =
            DependencyProperty.RegisterAttached( "Command", typeof( CheckBox ), typeof( Checked ), new
            PropertyMetadata( OnSetCommandCallback ) );

    public static readonly DependencyProperty CheckedCommandBehaviorProperty =
                DependencyProperty.RegisterAttached( "CheckedCommandBehavior", typeof( CheckedBehavior ), typeof( Checked ), null );

    private static void OnSetCommandCallback( DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e )
    {
        CheckBox element = dependencyObject as CheckBox;
        if( element != null )
        {
            CheckedBehavior behavior = GetOrCreateBehavior( element );
            behavior.Command = e.NewValue as ICommand;
        }
    }
    private static CheckedBehavior GetOrCreateBehavior( CheckBox element )
    {
        CheckedBehavior behavior = element.GetValue( CheckedCommandBehaviorProperty ) as CheckedBehavior;
        if( behavior == null )
        {
            behavior = new CheckedBehavior( element );
            element.SetValue( CheckedCommandBehaviorProperty, behavior );
        }

        return behavior;
    }
    public static CheckedBehavior GetCheckCommandBehavior( DependencyObject obj )
    {
        return (CheckedBehavior) obj.GetValue( CheckedCommandBehaviorProperty );
    }
    public static void SetCheckCommandBehavior( DependencyObject obj, CheckedBehavior value )
    {   
        obj.SetValue( CheckedCommandBehaviorProperty, value );
    }               

}

I used this article to get me started, but I'll readily admit this is over my head.

© Stack Overflow or respective owner

Related posts about .net-4.0

Related posts about Silverlight