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
Here is what I have got as my parameter.
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
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
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
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
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