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

Problem with bind Context Menù parameter

1 Answer 125 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Desartstudio
Top achievements
Rank 1
Desartstudio asked on 10 Nov 2011, 05:28 PM
Hi i have correct make a Context menù and binded it some example, 
after i have bind DelegateCommand some MyCommand,
And the Context menù is ok, but i whant pass to ContextMenù from the treeview Datacontext ,
the data, for call the right item when i click on the RadMenuItem, and take for example treeitemDatacontext.GuidId

In the telerik example use CommandParameter="{Binding Menu.ClickedListBoxItem, RelativeSource={RelativeSource Self}}"
but i dont undestand the Menu.ClickedListBoxItem item .

i have try, but not work, please help me 

regard 
<UserControl x:Class="Sicant.Modules.PSCDocument.Controls.TreeView"
                         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                         xmlns:example="clr-namespace:Sicant.Modules.PSCDocument"
                         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                         xmlns:tlkn="http://schemas.telerik.com/2008/xaml/presentation"
                         xmlns:Commands="clr-namespace:Sicant.Global.Commands;assembly=Sicant.Global" d:DesignHeight="600"
                         d:DesignWidth="300"
                         mc:Ignorable="d">
    <UserControl.Resources>
        <Style x:Key="SquareButton" TargetType="{x:Type Button}">
            <Setter Property="Button.Height" Value="25" />
            <Setter Property="Button.Width" Value="25" />
            <Setter Property="Margin" Value="2,2,2,2" />
        </Style>
        <Style x:Key="Menu" TargetType="Grid">
            <Setter Property="Height" Value="25" />
            <Setter Property="Width" Value="300" />
            <Setter Property="Background" Value="{StaticResource DarkBrush}" />
        </Style>
        <!--  nuovo per menù contestuale  -->
        <DataTemplate x:Key="Risorsa1_Template">
            <StackPanel Orientation="Horizontal">
                <Image Height="30" Source="/Sicant.Global;component/Images/Document.png" />
                <TextBlock Margin="5 0 0 0" Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="Risorsa2_Template">
            <StackPanel Orientation="Horizontal">
                <Image Height="30" Source="/Sicant.Global;component/Images/Mail_Closed.png" />
                <TextBlock Margin="5 0 0 0" Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
        <HierarchicalDataTemplate x:Key="Account_HierTemplate" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <Image Height="30" Source="/Sicant.Global;component/Images/male.png" />
                <TextBlock Margin="5 0 0 0" Text="{Binding Name}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="Container_HierTemplate" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <Image Height="30" Source="/Sicant.Global;component/Images/Metal_Crate.png" />
                <TextBlock Margin="5 0 0 0" Text="{Binding Name}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="PSC_HierTemplate" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <Image Height="30" Source="/Sicant.Global;component/Images/PSC.png" />
                <TextBlock Margin="5 0 0 0" Text="{Binding Name}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="Allegati_HierTemplate" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <Image Height="30" Source="/Sicant.Global;component/Images/Download.png" />
                <TextBlock Margin="5 0 0 0" Text="{Binding Name}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate x:Key="Add_HierTemplate" ItemsSource="{Binding Children}">
            <StackPanel Orientation="Horizontal">
                <Image Height="30" Source="/Sicant.Global;component/Images/add-tab.png" />
                <TextBlock Margin="5 0 0 0" Text="{Binding Name}" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <example:MyTemplateSelector x:Key="myTemplateSelector"
                                                                Template1="{StaticResource Risorsa1_Template}"
                                                                Template2="{StaticResource Risorsa2_Template}"
                                                                AccountTemplate="{StaticResource Account_HierTemplate}"
                                                                ContainerTemplate="{StaticResource Container_HierTemplate}"
                                                                PSCTemplate="{StaticResource PSC_HierTemplate}"
                                                                AllegatiTemplate="{StaticResource Allegati_HierTemplate}"
                                                                AddTemplate="{StaticResource Add_HierTemplate}"
                                                                />
        <DataTemplate x:Key="contextMenuItemTemplate">
            <tlkn:RadMenuItem Grid.Column="2"
                                        HorizontalAlignment="Left"
                                        VerticalAlignment="Center"
                                        Command="{Binding MyCommand}"
                                        CommandParameter="{
                                        Binding #What is the correct binding?#,
                                        RelativeSource={RelativeSource Self}
                                        }"
                                        Header="{Binding Header}"></tlkn:RadMenuItem>
 
        </DataTemplate>
        <Style x:Key="contextMenuItemStyle" TargetType="tlkn:RadMenuItem">
            <Setter Property="Width" Value="200" />
            <Setter Property="Height" Value="30" />
            <Setter Property="Icon" Value="{Binding Icon}" />
            <Setter Property="IsSeparator" Value="{Binding IsSeparator}" />
 
        </Style>
        <Style x:Key="treeViewStyle" TargetType="tlkn:RadTreeViewItem">
            <Setter Property="IsExpanded" Value="True" />
        </Style>
    </UserControl.Resources>
 
    <Grid>
        <DockPanel Name="dockTreePanel1" LastChildFill="True">
            <ContentControl Name="TopTreePanel"
                                            Height="AUTO"
                                            DockPanel.Dock="Top" />
            <ContentControl Name="MainTreePanel" DockPanel.Dock="Top" />
            <!--  contenitore di treeview complete  -->
            <StackPanel x:Name="Contenitore"
                                    Width="300"
                                    Margin="0"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Top"
                                    Orientation="Vertical">
                <!--  contenitore di treeview Archivi completa  -->
                <StackPanel x:Name="Contenitore_Archivi"
                                        Width="300"
                                        Margin="0"
                                        HorizontalAlignment="Left"
                                        VerticalAlignment="Top"
                                        Orientation="Vertical">
                    <!--  contenitore di Titolo Archivi  -->
                    <Grid HorizontalAlignment="Left" Style="{StaticResource Menu}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="28" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="48" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <Image Grid.Column="0"
                                     Width="16"
                                     Height="16"
                                     Margin="0"
                                     VerticalAlignment="Center"
                                     Source="{StaticResource ArchiveImg}" />
                        <TextBlock Grid.Column="1"
                                             Margin="15,0,0,0"
                                             HorizontalAlignment="Left"
                                             VerticalAlignment="Center"
                                             Text="Documento PSC" />
                        <Button Grid.Column="2"
                                        Width="20"
                                        Height="20"
                                        HorizontalAlignment="Right"
                                        VerticalAlignment="Center"
                                        Style="{StaticResource SquareButton}"
                                        ToolTip="Copia nell'archivio utente"
                                        Command="{x:Static Commands:PSCCommand.CmdPSC_PrintAll}">
                            <Image Width="14"
                                         Height="14"
                                         Source="{StaticResource PrintImg}" />
                        </Button>
                    </Grid>
                    <tlkn:RadTreeView x:Name="radTreeView"
                                                        ItemContainerStyle="{StaticResource treeViewStyle}"
                                                        ItemTemplateSelector="{StaticResource myTemplateSelector}">
                        <tlkn:RadContextMenu.ContextMenu>
                            <tlkn:RadContextMenu x:Name="radContextMenu"
                                                                     ItemContainerStyle="{StaticResource contextMenuItemStyle}"
                                                                     ItemTemplate="{StaticResource contextMenuItemTemplate}"
                                                                     Opened="RadContextMenu_Opened" />
                        </tlkn:RadContextMenu.ContextMenu>
                    </tlkn:RadTreeView>
                </StackPanel>
            </StackPanel>
        </DockPanel>
    </Grid>
</UserControl>

1 Answer, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 14 Nov 2011, 01:41 PM
Hello,

In the example we're using ExtendedContextMenu class which inherits RadContextMenu and also declares ClickedListBoxItem property where the clicked item is saved (it is found using GetClickedElement method).

I've attached to this thread a simple example which demonstrates how you can achieve the same with RadTreeView, please download the attachment and give it a try.

Regards,
Yana
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
ContextMenu
Asked by
Desartstudio
Top achievements
Rank 1
Answers by
Yana
Telerik team
Share this question
or