DataContext of RadContextMenu

3 posts, 0 answers
  1. Arnaud
    Arnaud avatar
    25 posts
    Member since:
    Jan 2013

    Posted 10 Jul 2013 Link to this post

    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. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 15 Jul 2013 Link to this post

    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 >>
  3. UI for WPF is Visual Studio 2017 Ready
  4. Arnaud
    Arnaud avatar
    25 posts
    Member since:
    Jan 2013

    Posted 17 Jul 2013 Link to this post

    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
Back to Top