mvvm ItemDoubleClick command

6 posts, 0 answers
  1. Florin
    Florin avatar
    3 posts
    Member since:
    Jul 2016

    Posted 05 Feb 2017 Link to this post

    I have  this user control view.

     

    <UserControl x:Class="TelerikWpfApp1.Views.MainMenuUserControlPanelBarStyle"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:TelerikWpfApp1.Views"
                 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
                 mc:Ignorable="d" 
                 d:DesignHeight="500" d:DesignWidth="250">
        <UserControl.DataContext>
            <Binding Mode="OneWay" Path="MainMenuUserContentPanelBarStyle" Source="{StaticResource Locator}"/>
        </UserControl.DataContext>
        <UserControl.Resources>
            <HierarchicalDataTemplate x:Key="Entity" ItemsSource="{Binding Randuri}">
                <StackPanel Orientation="Horizontal">
                    <Label x:Name="btn" Content="{Binding Titlu}" 
                               Margin="0,0,0,0"
                               />
                </StackPanel>
            </HierarchicalDataTemplate>
        </UserControl.Resources>

        
        <Grid>
                                 <!--ItemDoubleClick  ="{Binding PanelBarItemDoubleClick}"-->

            <telerik:RadPanelBar x:Name="radPanelBar"
                                 Margin="0,0" 
                                 ExpandMode="Multiple" 
                                 ItemsSource="{Binding UserMeniu}"
                                 ItemTemplate="{StaticResource Entity}"  
                                 SelectedItem="{Binding PanelBarSelectedItem, Mode=TwoWay}"
                                 
                                 >
            </telerik:RadPanelBar>

        </Grid>
        
    </UserControl>

     

    And the code for ItemDoubleClick ...:

            private void PanelBarItemDoubleClick(object sender,  Telerik.Windows.RadRoutedEventArgs e) {
                MessageBox.Show("It's working...");
            }

    When I run the project I get the error message "Unable to cast object of type 'System.Reflection.RuntimeEventInfo' to type 'System.Reflection.MethodInfo'."

    What can I do ?

    Thanks.

     

  2. Tanya
    Admin
    Tanya avatar
    906 posts

    Posted 08 Feb 2017 Link to this post

    Hello Florin,

    The WPF framework doesn't allow you binding to event handlers using the exposed property. More information on the approaches for attaching handlers to the events is available in this article.

    To attach an event handler from the view model, you can try using the EventToCommandBehavior.

    Hope this helps.

    Regards,
    Tanya
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  3. Florin
    Florin avatar
    3 posts
    Member since:
    Jul 2016

    Posted 08 Feb 2017 Link to this post

    Great.

    Thank you.

    I tried, following the example suggested, to use properties of clicked item:

            private void OnCustomCommandExecuted(object obj)  {

                var clickedItem = (obj as RadRoutedEventArgs).OriginalSource as ???; 
                if (clickedItem != null)
                {
                    MessageBox.Show("Clicked Item: " + clickedItem."SomeProperties");
                }
     

    ... but the clickedItem is null every time.

     

     

  4. Tanya
    Admin
    Tanya avatar
    906 posts

    Posted 13 Feb 2017 Link to this post

    Hi Florin,

    To pass any parameter in the handler of the command, you should set the command parameter properties. More information about these settings is available in the Command Parameters section.

    Regards,
    Tanya
    Telerik by Progress
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  5. Mohammad
    Mohammad avatar
    2 posts
    Member since:
    Sep 2006

    Posted 12 May 2019 Link to this post

    Hi
    When I use this code:
                RadTreeViewItem clickedItem = (obj as RadRoutedEventArgs).OriginalSource as RadTreeViewItem;
    clickedItem is null
    And when I use this:

                RadTreeView clickedItem = (obj as RadRoutedEventArgs).Source as RadTreeView;
    it is not null but I need to access to RadTreeViewItem for LoadOnDeman.
    my XAML:
           <telerik:RadTreeView x:Name="radTreeView" Grid.Row="1" Grid.ColumnSpan="3"
                    IsTextSearchEnabled="True" telerik:TextSearch.TextPath="Name" Padding="10"
                                IsLoadOnDemandEnabled="True"
                    Margin="0 0 1 0" ItemsSource="{Binding Models}">
                <telerik:RadTreeView.ItemTemplate>
                     <HierarchicalDataTemplate ItemsSource="{Binding Items}">
                           <StackPanel Orientation="Horizontal" Margin="2" >
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="500"></ColumnDefinition>
                                </Grid.ColumnDefinitions>

                                <TextBlock Text="{Binding Name}" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" />
                                <TextBlock Text="{Binding TitleTypeName}" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="50 0 0 0"/>
                            </Grid>
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </telerik:RadTreeView.ItemTemplate>
                      <telerik:EventToCommandBehavior.EventBindings>
                        <telerik:EventBinding RaiseOnHandledEvents="True" PassEventArgsToCommand="True" EventName="LoadOnDemand" Command="{Binding OnCustomCommand}" />
                    </telerik:EventToCommandBehavior.EventBindings>

    and I use MVVM .
    Thank you for your helping.
    Mahoodi

  6. Vladimir Stoyanov
    Admin
    Vladimir Stoyanov avatar
    686 posts

    Posted 15 May 2019 Link to this post

    Hello Mohammad,

    Thank you for the provided code. 

    I tested the described scenario on my end, however I was not able to reproduce the same result. That is why I am attaching the sample project, which I used for testing purposes. May I ask you to check it out and let me know, if I am missing something? Can you share what modifications are needed so that I can reproduce the same result in the attached project? This way I will check out the behavior on my end and investigate it.

    Regards,
    Vladimir Stoyanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top