Delegate command on CommandLoadedCommand not working

Jul 11, 2012 at 9:59 PM

Working on a WPF - Prism application. it is MVVM model.

From the constructor method I am delegating the command action  to 'OnUserComboboxLoadedCommand' method. Its not passing to it. 

1. ViewModel:

View model constructor:

 public LogInViewModel(IRegionManager regionManager, IEventAggregator eventAggregator)       {

UserComboboxLoadedCommand = new DelegateCommand<ComboBox>(OnUserComboboxLoadedCommand);

}

method:

private void OnUserComboboxLoadedCommand(ComboBox cmb)        {            _cmbBx = cmb;            _cmbBx.ItemsSource = _usersCollection;            _cmbBx.SelectionChanged +=new SelectionChangedEventHandler(_cmbBx_SelectionChanged);        }

2. Xaml View:

<ComboBox x:Name="cmb1" ToolTip="Drop to Select User"                          SelectedItem="{Binding SelectedComboItem,Mode=TwoWay}"                           cmd:ComboBoxLoaded.Command="{Binding UserComboboxLoadedCommand}"                           cmd:ComboBoxLoaded.CommandParameter="{Binding RelativeSource={RelativeSource Self}}"                          Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="6" Margin="35,1,3,0" Width="107" HorizontalAlignment="Left"                          >                    <ComboBox.ItemTemplate>                        <DataTemplate>                            <TextBlock x:Name="UserName" Text="{Binding UserName}" Foreground="Tomato"/>                        </DataTemplate>                    </ComboBox.ItemTemplate>                </ComboBox>

 

Code for Comboboxloaded Icommand:

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;
namespace CoM_MobileInfrastructureClass.Commands{    public class ComboBoxLoaded    {
        #region Readonly Properties
        private static readonly DependencyProperty Behavior =                                                              DependencyProperty.RegisterAttached(                                                                  "ComboBoxBehavior",                                                                  typeof(ComboBoxLoadedBehavior),                                                                  typeof(ComboBox),                                                                  null);
        public static readonly DependencyProperty Command =                                                             DependencyProperty.RegisterAttached(                                                                 "Command",                                                                 typeof(ICommand),                                                                 typeof(ComboBoxLoaded),                                                                 new PropertyMetadata(OnSetCommand));
        public static readonly DependencyProperty CommandParameter =                                                              DependencyProperty.RegisterAttached(                                                                  "CommandParameter",                                                                  typeof(object),                                                                  typeof(ComboBoxLoaded),                                                                  new PropertyMetadata(OnSetParameter));

        #endregion

        #region methods        public static void OnSetCommand(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)        {            var target = dependencyObject as ComboBox;            if (target == null)                return;
            GetOrCreateBehavior(target).Command = args.NewValue as ICommand;        }
        public static void OnSetParameter(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)        {            var target = dependencyObject as ComboBox;            if (target != null)            {                GetOrCreateBehavior(target).CommandParameter = args.NewValue;            }        }
        public static ComboBoxLoadedBehavior GetOrCreateBehavior(ComboBox control)        {            var behavior = control.GetValue(Behavior) as ComboBoxLoadedBehavior;            if (behavior == null)            {                behavior = new ComboBoxLoadedBehavior(control);                control.SetValue(Behavior, behavior);            }
            return behavior;        }
        public static void SetCommand(Control control, ICommand command)        {            control.SetValue(Command, command);        }
        public static ICommand GetCommand(Control control)        {            return control.GetValue(Command) as ICommand;        }
        public static void SetCommandParameter(Control control, object parameter)        {            control.SetValue(CommandParameter, parameter);        }
        public static object GetCommandParameter(Control buttonBase)        {            return buttonBase.GetValue(CommandParameter);        }

        #endregion

    }}

 

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using Microsoft.Practices.Prism.Commands;
namespace CoM_MobileInfrastructureClass.Commands{    public class ComboBoxLoadedBehavior : CommandBehaviorBase<ComboBox>    {
        public ComboBoxLoadedBehavior(ComboBox selectableObject)            : base(selectableObject)        {            selectableObject.Loaded += (sender, args) => ExecuteCommand();        }    }}

 

Jul 14, 2012 at 6:20 AM

Hi, can you provide a sample project which recreates the issue?

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com