ContextMenu binding in TreeView DataTemplate

2 posts, 0 answers
  1. Yu
    Yu avatar
    17 posts
    Member since:
    Jul 2012

    Posted 27 Dec 2012 Link to this post

    Hi,

    The following works when the context menu is declared in the TreeView directly.
    <telerik:RadTreeView Name="radTreeView" ExpanderStyle="{StaticResource ExpanderStyle}" ItemsSource="{Binding Path=EnvironmentMenuDataSource}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" ItemTemplate="{StaticResource EnvironmentMenu}">
                                        <telerik:RadContextMenu.ContextMenu>
                                            <telerik:RadContextMenu Opened="RadTreeViewContextMenuOpened">
                                                <telerik:RadMenuItem Header="{Binding Path=SelectedItem.ContextCommandName}"  Command="{Binding Path=ViewListCommand}" CommandParameter="{Binding SelectedItem}">
                                                    <telerik:RadMenuItem.Icon>
                                                        <Image Source="../Images/Menu/Blue.png" Stretch="Uniform" />
                                                    </telerik:RadMenuItem.Icon>
                                                </telerik:RadMenuItem>
                                            </telerik:RadContextMenu>
                                        </telerik:RadContextMenu.ContextMenu>
                                    </telerik:RadTreeView>

    However when placed my context menu inside the Data template as I only want a context menu with leave items,
    <DataTemplate x:Key="EntityMenu">
               <Grid>
                   <Grid.ColumnDefinitions>
                       <ColumnDefinition Width="0.35*"/>
                       <ColumnDefinition Width="0.65*"/>
                   </Grid.ColumnDefinitions>
                   <Image Source="{Binding ImageUrl, Mode=OneWay}" Width="16" Height="16"/>
                   <TextBlock Margin="3,0,0,0" Grid.Column="1" TextWrapping="Wrap" Text="{Binding Name, Mode=OneWay}">
                        <telerik:RadTreeView Name="radTreeView" ExpanderStyle="{StaticResource ExpanderStyle}" ItemsSource="{Binding Path=EnvironmentMenuDataSource}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" ItemTemplate="{StaticResource EnvironmentMenu}">
                                       <telerik:RadContextMenu.ContextMenu>
                                           <telerik:RadContextMenu Opened="RadTreeViewContextMenuOpened">
                                               <telerik:RadMenuItem Header="{Binding Path=SelectedItem.ContextCommandName}"  Command="{Binding Path=ViewListCommand}" CommandParameter="{Binding SelectedItem}">
                                                   <telerik:RadMenuItem.Icon>
                                                       <Image Source="../Images/Menu/Blue.png" Stretch="Uniform" />
                                                   </telerik:RadMenuItem.Icon>
                                               </telerik:RadMenuItem>
                                           </telerik:RadContextMenu>
                                       </telerik:RadContextMenu.ContextMenu>
                                   </telerik:RadTreeView>
                   </TextBlock>
               </Grid>
           </DataTemplate>
           <HierarchicalDataTemplate x:Key="EnvironmentMenu" ItemTemplate="{StaticResource EntityMenu}"
              ItemsSource="{Binding EntityMenus}">
               <TextBlock Text="{Binding Name}" />
           </HierarchicalDataTemplate>

    it does not work anymore. I tried to change the binding with the relativesouce but no luck. we are using the MVVM. The commands are in the ViewModel of the view.

    Do you have any examples with it?

    Thanks
  2. Pavel R. Pavlov
    Admin
    Pavel R. Pavlov avatar
    1183 posts

    Posted 27 Dec 2012 Link to this post

    Hi Yu,

    Let me get straight to your question. I have noticed that you place the RadTreeView control in a TextBlock. Can you please explain why you do this and how do you expect it to work as this is not a common scenario? Can you please try to wrap the TextBlock and the RadTreeView (when it is outside the TextBlock) control together in a Grid and see if it is working?

    Regarding your last question you can read this article where we use the MVVM approach or you can take a look at our online demonstration project.

    Regards,
    Pavel R. Pavlov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top