Getting CommandParameter

7 posts, 0 answers
  1. Deepak
    Deepak avatar
    8 posts
    Member since:
    Jul 2014

    Posted 12 Aug 2014 Link to this post

    Hi all,

    I am trying to get the parameter out of CommandParameter on a button click. I got the parameter values in the EXECUTE method. The issue is the type of my parameter is not what I expected and also I am unable to cast it. 

    This is what I have as my parameter value.



    Now, I can see the type of my parameter is Telerik.Windows.Controls.Primitives.SelectionChanger<object>​, but I am not able to cast it in codebehind as I cannot see Telerik.Windows.Controls.Primitives.SelectionChanger.  
    Not sure if I am able to put my question correctly and completely. Please let me know if you need more information.

    Regards,
    Deepak
  2. Deepak
    Deepak avatar
    8 posts
    Member since:
    Jul 2014

    Posted 12 Aug 2014 in reply to Deepak Link to this post

    Sorry guys missed the screenshot. 
    Here is what I have got as my parameter.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Milena
    Admin
    Milena avatar
    204 posts

    Posted 13 Aug 2014 Link to this post

    Hello Deepak, 

    Unfortunately, given only the provided information I am not able to understand your scenario. Can you elaborate more by providing us more detailed information about how you implement your command – a sample project or code snippet would be great. Also you can tell us what kind of parameters you expect to get, so that we can help you by resolving the issue.

    Thank you in advance for your cooperation.

    Regards,
    Milena
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  5. MHS
    MHS avatar
    1 posts
    Member since:
    Aug 2011

    Posted 07 Jul 2015 Link to this post

    Hi all,

    I have the same problem with RadListBox, MVVM and commands.

    <telerik:RadListBox Name="taskListBox"
                        Width="640" Height="230" 
                        Background="White"
                        HorizontalAlignment="Left" VerticalAlignment="Top" 
                        ItemsSource="{Binding Tasks}"
                        ItemTemplate="{StaticResource ListBoxCustomTemplate}"
                        SelectionMode="Multiple">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="SelectionChanged">
                                        <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=taskListBox, Path=SelectedItems}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>   
                            </telerik:RadListBox>

    The type of SelectedItems is -  obj Count = 1 object {Telerik.Windows.Controls.Primitives.SelectionChanger<object>}

    I have not found this type !?!

    Is it the right way using the selection change event ?

    Thx a lot.

    Michael

  6. Kalin
    Admin
    Kalin avatar
    1209 posts

    Posted 10 Jul 2015 Link to this post

    Hello Michael,

    What I can suggest you would to either use our EventToCommandBehavior and directly pass the event args to the command:

    <telerik:RadListBox>
        <telerik:EventToCommandBehavior.EventBindings>
            <telerik:EventBinding EventName="SelectionChanged"
                                  Command="{Binding SelectionChangedCommand}"
                                  PassEventArgsToCommand="True" />
        </telerik:EventToCommandBehavior.EventBindings>
    </telerik:RadListBox>

    Or use the new SelectedItemsSource property that will allow you to bind an ObservableCollection of objects to the SelectedItems collection of the control.

    Hope this helps.

    Regards,
    Kalin
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Fernando
    Fernando avatar
    1 posts
    Member since:
    Dec 2015

    Posted 21 Sep in reply to MHS Link to this post

    MHS said:

    Hi all,

    I have the same problem with RadListBox, MVVM and commands.

    <telerik:RadListBox Name="taskListBox"
                        Width="640" Height="230" 
                        Background="White"
                        HorizontalAlignment="Left" VerticalAlignment="Top" 
                        ItemsSource="{Binding Tasks}"
                        ItemTemplate="{StaticResource ListBoxCustomTemplate}"
                        SelectionMode="Multiple">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="SelectionChanged">
                                        <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=taskListBox, Path=SelectedItems}"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>   
                            </telerik:RadListBox>

    The type of SelectedItems is -  obj Count = 1 object {Telerik.Windows.Controls.Primitives.SelectionChanger<object>}

    I have not found this type !?!

    Is it the right way using the selection change event ?

    Thx a lot.

    Michael

    You can cast the SelectionChanger<object> to IEnumerable<object>, and then iterate over the collection, casting each item to the type of RadListBox's items, or cast all elements at the same time using System.Linq.Cast

  8. Lance | Tech Support Engineer, Sr.
    Admin
    Lance | Tech Support Engineer, Sr. avatar
    138 posts

    Posted 22 Sep Link to this post

    Hi Fernando,

    What Kalin provided below is the solution for the EventTigger Behavior approach, by setting PassEventArgsToCommand="True" you will get the event args passed to the command from which you can use in the command as if it were in an event handler.

    Alternatively, the other option he mentions is to leverage our new attached property: SelectedItemsSource. With this you can directly see changes in the viewmodel and act on the bound collection directly.

    I've attached a small demo I built for you that shows one way you can use it. See MainPage.xaml and MainPageViewModel.cs. I've also attached a screenshot of the app at runtime. Here's a snippet from the view model on where I show how to detect what items were selected or deselected:

    /// <summary>
    /// This is where you can detect what was selected or deselected in the RadListBox
    /// because the ObservableCollection is two-way bound to the SelectedItemsSource property
    /// </summary>
    private void SelectedCars_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
         if (e.NewItems != null && e.NewItems.Count > 0)
         {
            var addedCar = e.NewItems[0] as Car;
            Output.Add($"You selected a car - Make: {addedCar?.Make}, Year {addedCar?.Year}");
          }
     
         if (e.OldItems != null && e.OldItems.Count > 0)
         {
             var removedCar = e.OldItems[0] as Car;
             Output.Add($"You unselected a car - Make: {removedCar?.Make}, Year {removedCar?.Year}");
         }
    }


    If you're still having trouble, please change the attached demo to replicate the problem. Then open a support ticket, attach the repro project and we'll investigate further.

    Regards,
    Lance | Tech Support Engineer, Sr.
    Telerik by Progress
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WPF is Visual Studio 2017 Ready