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

DataContext of RadContextMenu

3 Answers 378 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

3 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
0
Johnathan
Top achievements
Rank 1
Iron
Iron
Iron
answered on 22 Oct 2025, 03:42 PM

I'm not sure I agree with the assertion that 'You are not able to use Binding for DataContext in HierarchicalDataTemplate'. I say this because I can use a standard ContextMenu (System.Windows.Controls) and the binding works just fine, but the RadContextMenu seems to have the issue in the HierarchicalDataTemplate.  Can you clarify your assertion?

 

<UserControl x:Class="IEToolkit.View.Widget.Dock.Explorer"
             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:IEToolkit.View.Widget.Dock"
			 xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
	<UserControl.Resources>
		<telerik:RadContextMenu x:Key="ExplorerContextMenuProject" >
			<telerik:RadMenuItem Header="Remove Project" />
			<telerik:RadMenuItem IsSeparator="True" />
			<telerik:RadMenuItem Header="Add Module"
								 IsEnabled="{Binding DataContext.FormAddProject.Visible, RelativeSource={RelativeSource AncestorType=local:Explorer}}"/>
		</telerik:RadContextMenu>

		<ContextMenu x:Key="ctx">
			<MenuItem Header="Remove Project" />
			<Separator />
			<MenuItem Header="Add Module"
					  IsEnabled="{Binding DataContext.FormAddProject.Visible, RelativeSource={RelativeSource AncestorType=local:Explorer}}"/>
		</ContextMenu>

		<DataTemplate x:Key="ExplorerTreeViewProjects">
			<Grid>
				<Grid.Style>
					<Style TargetType="Grid">
						<Setter Property="ContextMenu" Value="{StaticResource ctx}" />
					</Style>
				</Grid.Style>
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="*" />
				</Grid.ColumnDefinitions>

				<TextBlock Text="{Binding Number}" />
			</Grid>
		</DataTemplate>
		<HierarchicalDataTemplate x:Key="ExplorerTreeViewSites" 
						  ItemTemplate="{StaticResource ExplorerTreeViewProjects}" 
						  ItemsSource="{Binding Projects}">
			<Grid>
				<Grid.Style>
					<Style TargetType="Grid">
						<Setter Property="telerik:RadContextMenu.ContextMenu" Value="{StaticResource ExplorerContextMenuProject}" />
					</Style>
				</Grid.Style>
				<Grid.ColumnDefinitions>
					<ColumnDefinition Width="*" />
				</Grid.ColumnDefinitions>

				<TextBlock Text="{Binding Name}" />
			</Grid>
		</HierarchicalDataTemplate>
	</UserControl.Resources>
	<Grid>
		<telerik:RadTreeView IsVirtualizing="False"
							 ItemsSource="{Binding Sites}"
							 ItemTemplate="{StaticResource ExplorerTreeViewSites}">
			
		</telerik:RadTreeView>
	</Grid>
</UserControl>


Tags
ContextMenu
Asked by
Olivier
Top achievements
Rank 1
Answers by
Rosen Vladimirov
Telerik team
Olivier
Top achievements
Rank 1
Johnathan
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or