开发者

Create a contextmenu with tree structure in c#

开发者 https://www.devze.com 2023-04-03 01:23 出处:网络
I\'m struggling while try to create a ContextMenu in C# (WPF) with a hierarchical structure. The source is a simply List which contains these:

I'm struggling while try to create a ContextMenu in C# (WPF) with a hierarchical structure. The source is a simply List which contains these:

\\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

Basically I have to create a thee menu as this:

-root 
   开发者_如何转开发   -folderA 
               -programA.exe
               -programB.exe
               -programC.exe

I have few experience about WPF, I know that this can be done combining ContextMenu and MenuItem. Any hints?


I'm assuming you're making view models for each TreeViewItem? If so, then it simply becomes a matter of assigning a different <HierarchicalDataTemplate> resources to each view model type, and then each template can have it's own <ContentMenu>. For example, this is from one of my projects where I'm making a SQL Object Explorer like TreeView:

        <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>

I've created a different ViewModel for each TreeViewItem. In your case, you would probably have a RootViewModel, FolderViewModel, and then FileViewModel. Each would have it's own template and then you can style each as necesary.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号