Custom command for Silver light control

While doing one requirement in Silverlight I found one issue for some controls like ListBox , we cannot Assign Commands to from Data source.  Some controls like Button we can assign click event to Command directly.  
<Button Content="Load Employee" Height="23" HorizontalAlignment="Left" Margin="12,345,0,0"
                Name="btnLoadEmployee" VerticalAlignment="Top" Width="283"
                 Command="{Binding Path=LoadEmployeeCommand}"/>

But this will not happened for List Box Selection Change Event. For implementing this one I have created a Custom Command.

/// <summary>
    /// The below class represents the behavior
    /// </summary>
        public class ListBoxSelectionChangedCommandBehavior :
            CommandBehaviorBase<ListBox>
        {
            public ListBoxSelectionChangedCommandBehavior(ListBox lstTarget)
                : base(lstTarget)
            {
                    lstTarget.SelectionChanged += OnListBoxSelectionChanged;
            }
            private void OnListBoxSelectionChanged(object s, SelectionChangedEventArgs e)
            {
                this.ExecuteCommand();
            }
        }

    /// <summary>
    /// The below class define the Attached Property
    /// </summary>
        public static class ListBoxSelectionChanged
        {

            public static object GetCommandParameter(ListBox listBox)
            {
                return (object)listBox.GetValue(CommandParameterProperty);
            }

            public static void SetCommandParameter(ListBox listBox, object value)
            {
                listBox.SetValue(CommandParameterProperty, value);
            }
            
            /// <summary>
            /// The Command Behavior Property
            /// </summary>
            public static readonly DependencyProperty CommandParameterProperty =
                DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(ListBoxSelectionChanged), 
                new PropertyMetadata(OnSetCustomCommandParameterCallback));

            public static ICommand GetCommand(ListBox listBox)
            {
                return (ICommand)listBox.GetValue(CommandProperty);
            }

            public static void SetCommand(ListBox listBox, ICommand value)
            {
                listBox.SetValue(CommandProperty, value);
            }

            /// <summary>
            /// COmmand to Execute a CLick Event
            /// </summary>
            public static readonly DependencyProperty CommandProperty =
                DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ListBoxSelectionChanged),
                new PropertyMetadata(OnSetCustomCommandCallback));

           
            public static ListBoxSelectionChangedCommandBehavior GetClickCommandBehavior(DependencyObject obj)
            {
                return (ListBoxSelectionChangedCommandBehavior)obj.GetValue(ClickCommandBehaviorProperty);
            }

            public static void SetClickCommandBehavior(DependencyObject obj, ListBoxSelectionChangedCommandBehavior value)
            {
                obj.SetValue(ClickCommandBehaviorProperty, value);
            }

            // Using a DependencyProperty as the backing store for ClickCommandBehavior.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty ClickCommandBehaviorProperty =
                DependencyProperty.RegisterAttached("ClickCommandBehavior",
                typeof(ListBoxSelectionChangedCommandBehavior),
                typeof(ListBoxSelectionChanged), null);

            private static ListBoxSelectionChangedCommandBehavior GetorCreateListBoxBehavior(ListBox listBox)
            {
                var behavior = listBox.GetValue(ClickCommandBehaviorProperty) as
                    ListBoxSelectionChangedCommandBehavior;
                if (behavior == null)
                {
                    behavior = new ListBoxSelectionChangedCommandBehavior(listBox);
                    listBox.SetValue(ClickCommandBehaviorProperty, behavior);
                }
                return behavior;
            }


            private static void OnSetCustomCommandCallback(DependencyObject dep, DependencyPropertyChangedEventArgs e)
            {
                var listBox = dep as ListBox;
                if (listBox != null)
                {
                    ListBoxSelectionChangedCommandBehavior behavior = GetorCreateListBoxBehavior(listBox);
                        behavior.Command = e.NewValue as ICommand;
                }
            }

            private static void OnSetCustomCommandParameterCallback(DependencyObject dep,
                DependencyPropertyChangedEventArgs e)
            {
                var listBox = dep as ListBox;
                if (listBox != null)
                {
                    ListBoxSelectionChangedCommandBehavior behavior = GetorCreateListBoxBehavior(listBox); ;
                        behavior.CommandParameter = e.NewValue;
                }
            }
        }

After this I have given this in My .Xaml Code.

Add Reference for my custom class
xmlns:srcData="clr-namespace:SL4_CustomCommands"        
    mc:Ignorable="d"
Implement it in my Silverlight code.
<ListBox Height="275" HorizontalAlignment="Left" Margin="10,56,0,0" 
      Name="lstEmployees" VerticalAlignment="Top" Width="285"                     ItemsSource="{Binding Path=Employees}" ItemTemplate="{StaticResource EmpTemplate}"
 srcData:ListBoxSelectionChanged.Command="{Binding FilterEmployeeCommand}"
srcData:ListBoxSelectionChanged.CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem,Mode=TwoWay}"/>


No comments:

Post a Comment