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

Getting CommandParameter

6 Answers 1060 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Deepak
Top achievements
Rank 1
Deepak asked on 12 Aug 2014, 11:11 AM
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

6 Answers, 1 is accepted

Sort by
0
Deepak
Top achievements
Rank 1
answered on 12 Aug 2014, 11:17 AM
Sorry guys missed the screenshot. 
Here is what I have got as my parameter.
0
Milena
Telerik team
answered on 13 Aug 2014, 12:09 PM
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.
 
0
MHS
Top achievements
Rank 1
answered on 07 Jul 2015, 10:36 AM

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

0
Kalin
Telerik team
answered on 10 Jul 2015, 09:49 AM
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
0
Fernando
Top achievements
Rank 1
answered on 21 Sep 2016, 03:16 PM
[quote]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

[/quote]

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

0
Lance | Senior Manager Technical Support
Telerik team
answered on 22 Sep 2016, 07:30 PM
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.
Tags
Buttons
Asked by
Deepak
Top achievements
Rank 1
Answers by
Deepak
Top achievements
Rank 1
Milena
Telerik team
MHS
Top achievements
Rank 1
Kalin
Telerik team
Fernando
Top achievements
Rank 1
Lance | Senior Manager Technical Support
Telerik team
Share this question
or