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

ContextMenu binding in TreeView DataTemplate

1 Answer 169 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Yu
Top achievements
Rank 1
Yu asked on 27 Dec 2012, 12:43 PM
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

1 Answer, 1 is accepted

Sort by
0
Pavel R. Pavlov
Telerik team
answered on 27 Dec 2012, 03:25 PM
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.

Tags
TreeView
Asked by
Yu
Top achievements
Rank 1
Answers by
Pavel R. Pavlov
Telerik team
Share this question
or