This is a migrated thread and some comments may be shown as answers.

SelectionChanged event and Attached Behavior

4 Answers 498 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 16 Sep 2009, 08:45 AM
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?

4 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 22 Sep 2009, 03:12 PM
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.
0
James
Top achievements
Rank 1
answered on 23 Sep 2009, 01:46 AM
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);


0
Accepted
Kaloyan
Telerik team
answered on 25 Sep 2009, 10:55 AM
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.
0
James
Top achievements
Rank 1
answered on 27 Sep 2009, 02:32 AM
yes this works thank you
Tags
ComboBox
Asked by
James
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
James
Top achievements
Rank 1
Share this question
or