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

DataContext of RadContextMenu

2 Answers 280 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Olivier
Top achievements
Rank 1
Olivier asked on 10 Jul 2013, 07:07 AM
Hello all
I have difficulty with RadContextMenu and particulary DataContext property.
I have a RadWindow ("mainWindow") who contain a RadTreeView. RadTreeView expose Items by HierarchicalDataTemplate, that this items who contains RadContextMenu.
My problem : DataContext of RadContextMenu is always DataContext of Item and I want the DataContext of the RadWindow.
I show you my XAML :
<telerik:RadWindow x:Class="Vue.MainWindow" x:Name="mainWindow"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                        
                   xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<Grid x:Name="mainGrid">
   <telerik:RadTreeView x:Name="radTreeView" Grid.Row="1" ItemsSource="{Binding TypeDatabases}"
      <telerik:RadTreeView.Resources>
                  <HierarchicalDataTemplate DataType="{x:Type Modele:EntiteDatabase}" ItemsSource="{Binding Tables}">
                            <StackPanel Orientation="Horizontal" Height="25">
                                <Image Source="{Binding SourceIcone}" Stretch="Fill" Height="16"/>
                                <TextBlock Text="{Binding Description}" TextAlignment="Left" VerticalAlignment="Center" />
                                <telerik:RadContextMenu.ContextMenu>
                                    <telerik:RadContextMenu InheritDataContext="False" DataContext="{Binding DataContext, ElementName=mainWindow}">
                                        <telerik:RadMenuItem Header="Connexion" Command="{Binding OpenDatabase}" />
                                        <telerik:RadMenuItem Header="Déconnexion" Command="{Binding CloseDatabase}" />
                                    </telerik:RadContextMenu>
                                </telerik:RadContextMenu.ContextMenu>
                            </StackPanel>
                        </HierarchicalDataTemplate>
      </telerik:RadTreeView.Resources>
   </telerik:RadTreeView>
</Grid>
</telerik:RadWindow>
In this solution, I have an error : "cannot find ElementName=mainWindow" and I don't understand
I tried others methods to retrieve the DataContext (Tag in my StackPanel, ....)
Thank to you for help

PS : I am sorry for my english

2 Answers, 1 is accepted

Sort by
0
Rosen Vladimirov
Telerik team
answered on 15 Jul 2013, 05:36 AM
Hi Arnaud,

You are not able to use Binding for DataContext in HierarchicalDataTemplate. I've prepared a simple project, which is based on your requirements. Please find it attached and inform us if you have any problems or concerns.

Regards,
Rosen Vladimirov
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Olivier
Top achievements
Rank 1
answered on 17 Jul 2013, 07:51 AM
Thanks for reply
I tried to have a DataContext per HierarchicalDataTemplate. 
I will apply one DataContext for my RadTreeView and play with Visibility for displaying commands depending object selected.
Thanks again
Tags
ContextMenu
Asked by
Olivier
Top achievements
Rank 1
Answers by
Rosen Vladimirov
Telerik team
Olivier
Top achievements
Rank 1
Share this question
or