在c#中创建具有树结构的上下文菜单

时间:2011-09-07 14:40:08

标签: c# wpf contextmenu menuitem hierarchical

我在尝试使用层次结构在C#(WPF)中创建ContextMenu时遇到了困难。源是一个简单的List,其中包含:

\\root\folderA\programA.exe
\\root\folderA\programB.exe
\\root\folderA\programC.exe
\\root\folderB\programA.exe
\\root\folderB\programE.exe
\\root\programF.exe
\\root\programG.exe
\\root\programH.exe

基本上我必须创建一个菜单:

-root 
      -folderA 
               -programA.exe
               -programB.exe
               -programC.exe

我对WPF的经验很少,我知道这可以结合ContextMenu和MenuItem来完成。任何提示?

1 个答案:

答案 0 :(得分:0)

我假设您正在为每个TreeViewItem创建视图模型?如果是这样,那么只需为每个视图模型类型分配不同的<HierarchicalDataTemplate>资源,然后每个模板都可以拥有它自己的<ContentMenu>。例如,这是我的一个项目,我正在创建一个像TreeView一样的SQL对象资源管理器:

        <TreeView.Resources>
            <!-- Brushes for the selected item -->
            <LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFDCEBFC" Offset="0"/>
                <GradientStop Color="#FFC1DBFC" Offset="1"/>
            </LinearGradientBrush>
            <LinearGradientBrush x:Key="{x:Static SystemColors.ControlBrushKey}" EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFF8F8F8" Offset="0"/>
                <GradientStop Color="#FFE5E5E5" Offset="1"/>
            </LinearGradientBrush>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
            <HierarchicalDataTemplate DataType="{x:Type vm:ServerViewModel}" ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal" Margin="2,1,5,2">
                    <StackPanel.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Refresh">
                                <MenuItem.Icon>
                                    <Image Source="/Content/ServerExplorer/RefreshIcon.png"></Image>
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </StackPanel.ContextMenu>
                    <Grid Margin="0,0,3,0">
                        <Image Name="icon" Source="/Content/ServerExplorer/ServerIcon.png" Height="16" Width="16"></Image>
                    </Grid>
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type vm:DatabaseViewModel}" ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal" Margin="2,1,5,2">
                    <StackPanel.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Refresh">
                                <MenuItem.Icon>
                                    <Image Source="/Content/ServerExplorer/RefreshIcon.png"></Image>
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </StackPanel.ContextMenu>
                    <Grid Margin="0,0,3,0">
                        <Image Name="icon" Source="/Content/ServerExplorer/DatabaseIcon.png" Height="16" Width="16"></Image>
                    </Grid>
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </HierarchicalDataTemplate>

            <DataTemplate DataType="{x:Type vm:TableViewModel}">
                <StackPanel Orientation="Horizontal" Margin="2,1,5,2">
                    <StackPanel.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Script Records" Click="ScriptRecords_Click">
                                <MenuItem.Icon>
                                    <Image Source="/Content/ServerExplorer/ScriptIcon.png"></Image>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Script Table">
                                <MenuItem.Icon>
                                    <Image Source="/Content/ServerExplorer/ScriptIcon.png"></Image>
                                </MenuItem.Icon>
                            </MenuItem>
                            <MenuItem Header="Create Replication">
                                <MenuItem.Icon>
                                    <Image Source="/Content/ServerExplorer/ReplicationIcon.png"></Image>
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </StackPanel.ContextMenu>
                    <Grid Margin="0,0,3,0">
                        <Image Name="icon" Source="/Content/ServerExplorer/TableIcon.png" Height="16" Width="16"></Image>
                    </Grid>
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>

            <DataTemplate DataType="{x:Type vm:StoredProcedureViewModel}">
                <StackPanel Orientation="Horizontal" Margin="2,1,5,2">
                    <StackPanel.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Script Procedure">
                                <MenuItem.Icon>
                                    <Image Source="/Content/ServerExplorer/ScriptIcon.png"></Image>
                                </MenuItem.Icon>
                            </MenuItem>
                        </ContextMenu>
                    </StackPanel.ContextMenu>
                    <Grid Margin="0,0,3,0">
                        <Image Name="icon" Source="/Content/ServerExplorer/StoredProcedureIcon.png" Height="16" Width="16"></Image>
                    </Grid>
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
       </TreeView.Resources>

我为每个TreeViewItem创建了一个不同的ViewModel。在您的情况下,您可能有一个RootViewModel,FolderViewModel,然后是FileViewModel。每个人都有自己的模板,然后你可以根据需要设置每个模板。