SelectionChanged event and Attached Behavior

5 posts, 1 answers
  1. James
    James avatar
    47 posts
    Member since:
    Jun 2009

    Posted 16 Sep 2009 Link to this post

    I use the build 2009.2.812. I have an attached behavior to handle the RadComboBox.SelectionChanged event to implement command and command parameter binding like this:
     <telerik:RadComboBox x:Name="rcbActionResponse"
      ItemsSource="{Binding ActionResponseLookup}" DisplayMemberPath="StdActionDescription" SelectedValuePath="StdActionID"
      Behavior:SelectionChanged.CommandParameter="{Binding SelectedItem, ElementName=rcbActionResponse}                                  
      Behavior:SelectionChanged.Command="{Binding ActionChangedCommand}"/>

    in constuctor of the viewmodel:
               ActionChangedCommand = new DelegateCommand<object>(ActionChanged);



    private void ActionChanged(object arg)
        {
    //logic to handle SelectionChanged event, expect arg is the current selected item of the RadComboBox
    }




    While I noticed the ActionChanged method was always fired before the property changed callback of the attached property CommandParameter - that make the arg parameter is not the current selected item but the old selected item

    Any idea of this problem?
  2. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 22 Sep 2009 Link to this post

    Hi James,

    I am afraid that the provided details are not enough for us to pinpoint the source of the problem. Could you please share some more code (XAML + codebehind). It will be really of great help to us to be able to reproduce the problem locally and try to locate its source.

    Best wishes,
    Kaloyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. James
    James avatar
    47 posts
    Member since:
    Jun 2009

    Posted 22 Sep 2009 Link to this post

    Kaloyan,
    the xaml about he RadBomBox has been pasted in my first post. the code-behide is not related. the other code is in the ViewModel.
    Below is the code of attached behavior which uses Prism:

    using Microsoft.Practices.Composite.Presentation.Commands;
    using Telerik.Windows.Controls;



    public class RadComboBoxSelectionChangedBehavior : CommandBehaviorBase<RadComboBox>
      {
        public RadComboBoxSelectionChangedBehavior(RadComboBox control)
          : base(control)
        {
          control.SelectionChanged += new Telerik.Windows.Controls.SelectionChangedEventHandler(control_SelectionChanged);
        }

        void control_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
        {
            
          base.ExecuteCommand();
        }

      }

      public static class SelectionChanged
      {
        private static readonly DependencyProperty RadComboBoxSelectionChangedBehaviorProperty
          = DependencyProperty.RegisterAttached(
          "RadComboBoxSelectionChangedBehavior",
          typeof(RadComboBoxSelectionChangedBehavior),
          typeof(SelectionChanged),
          null);

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

        public static ICommand GetCommand(RadComboBox control)
        {
          return control.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommand(RadComboBox control, ICommand value)
        {
          control.SetValue(CommandProperty, value);
        }

        public static readonly DependencyProperty CommandParameterProperty
          = DependencyProperty.RegisterAttached(
         "CommandParameter",
         typeof(object),
         typeof(SelectionChanged),
         new PropertyMetadata(OnSetCommandParameterCallback));

        public static void SetCommandParameter(RadComboBox control, object parameter)
        {
          control.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(RadComboBox control)
        {
          return control.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback
          (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
          RadComboBox control = dependencyObject as RadComboBox;
          if (control != null)
          {
            RadComboBoxSelectionChangedBehavior behavior = GetOrCreateBehavior(control);
            behavior.Command = e.NewValue as ICommand;
          }
        }

        private static void OnSetCommandParameterCallback
           (DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
          RadComboBox control = dependencyObject as RadComboBox;
          if (control != null)
          {
            RadComboBoxSelectionChangedBehavior behavior = GetOrCreateBehavior(control);
            behavior.CommandParameter = e.NewValue;
          }
        }

        private static RadComboBoxSelectionChangedBehavior GetOrCreateBehavior(RadComboBox control)
        {
          RadComboBoxSelectionChangedBehavior behavior =
              control.GetValue(RadComboBoxSelectionChangedBehaviorProperty) as RadComboBoxSelectionChangedBehavior;
          if (behavior == null)
          {
            behavior = new RadComboBoxSelectionChangedBehavior(control);
            control.SetValue(RadComboBoxSelectionChangedBehaviorProperty, behavior);
          }
          return behavior;
        }

      }

    Here is the property in ViewModel:
     public DelegateCommand<object> ActionChangedCommand { get; private set; }

    in the constructor of the ViewModel:
     ActionChangedCommand = new DelegateCommand<object>(ActionChanged);


  4. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 25 Sep 2009 Link to this post

    Hello James,

    This is the expected behavior, due to the Binding system in the Silverlight CLR. To overcome this issue you can set the CommandParameter in the control_SelectionChanger eventhandler:

    private void control_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e) 
            { 
                this.CommandParameter = e.AddedItems[0]; 
     
                base.ExecuteCommand(); 
            } 


    Regards,
    Kaloyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. James
    James avatar
    47 posts
    Member since:
    Jun 2009

    Posted 26 Sep 2009 Link to this post

    yes this works thank you
Back to Top