将 WPF TreeView 转换为菜单

发布于 2024-10-31 22:01:05 字数 2728 浏览 9 评论 0 原文

我在将此工作树视图转换为菜单时遇到问题。

该树视图显示正确。

<UserControl.DataContext>
    <ObjectDataProvider ObjectType="{x:Type storage:Database}"
                        MethodName="GetGroups"/>
</UserControl.DataContext>
<UserControl.Resources>
    <converters:PathToNameConverter x:Key="pathToNameConverter" />
</UserControl.Resources>
<TreeView Name="TreeViewMain" ItemsSource="{Binding Path=.}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type storage:File}">
            <TreeViewItem Header="{Binding Path=Name, Mode=TwoWay}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type storage:Group}">
            <TreeViewItem>
                <TreeViewItem.Header>
                    <StackPanel Grid.Row="0" Orientation="Horizontal">
                        <Image Source="../Images/Core/16x16/Folder.png" />
                        <TextBlock Text="{Binding Path=Name, Mode=TwoWay}"/>
                    </StackPanel>
                </TreeViewItem.Header>
                <ItemsControl ItemsSource="{Binding Path=Groups}" />
                <ItemsControl ItemsSource="{Binding Path=Files}" />
            </TreeViewItem>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

我把所有内容都改为菜单,但不起作用。有组名称和图像,箭头指向右侧,看起来它正在工作,但当我单击它时它不会展开。

<UserControl.DataContext>
    <ObjectDataProvider ObjectType="{x:Type storage:Database}"
                        MethodName="GetGroups"/>
</UserControl.DataContext>
<Menu Name="MenuMain" ItemsSource="{Binding Path=.}">
    <Menu.Resources>
        <HierarchicalDataTemplate DataType="{x:Type storage:File}">
            <MenuItem Header="{Binding Path=Name, Mode=TwoWay}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type storage:Group}">
            <MenuItem>
                <MenuItem.Header>
                    <StackPanel Grid.Row="0" Orientation="Horizontal">
                        <Image Source="../Images/Core/16x16/Folder.png" />
                        <TextBlock Text="{Binding Path=Name, Mode=TwoWay}" />
                    </StackPanel>
                </MenuItem.Header>
                <ItemsControl ItemsSource="{Binding Path=Groups}" />
                <ItemsControl ItemsSource="{Binding Path=Files}" />
            </MenuItem>
        </HierarchicalDataTemplate>
    </Menu.Resources>
</Menu>

我做错了什么?

I'm having trouble converting this working treeview into a menu.

This treeview displays correctly.

<UserControl.DataContext>
    <ObjectDataProvider ObjectType="{x:Type storage:Database}"
                        MethodName="GetGroups"/>
</UserControl.DataContext>
<UserControl.Resources>
    <converters:PathToNameConverter x:Key="pathToNameConverter" />
</UserControl.Resources>
<TreeView Name="TreeViewMain" ItemsSource="{Binding Path=.}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type storage:File}">
            <TreeViewItem Header="{Binding Path=Name, Mode=TwoWay}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type storage:Group}">
            <TreeViewItem>
                <TreeViewItem.Header>
                    <StackPanel Grid.Row="0" Orientation="Horizontal">
                        <Image Source="../Images/Core/16x16/Folder.png" />
                        <TextBlock Text="{Binding Path=Name, Mode=TwoWay}"/>
                    </StackPanel>
                </TreeViewItem.Header>
                <ItemsControl ItemsSource="{Binding Path=Groups}" />
                <ItemsControl ItemsSource="{Binding Path=Files}" />
            </TreeViewItem>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

I change everything to Menu, and it doesn't work. There's the group name and image with an arrow pointing to the right, and it looks like it's working, but it doesn't expand when I click on it.

<UserControl.DataContext>
    <ObjectDataProvider ObjectType="{x:Type storage:Database}"
                        MethodName="GetGroups"/>
</UserControl.DataContext>
<Menu Name="MenuMain" ItemsSource="{Binding Path=.}">
    <Menu.Resources>
        <HierarchicalDataTemplate DataType="{x:Type storage:File}">
            <MenuItem Header="{Binding Path=Name, Mode=TwoWay}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type storage:Group}">
            <MenuItem>
                <MenuItem.Header>
                    <StackPanel Grid.Row="0" Orientation="Horizontal">
                        <Image Source="../Images/Core/16x16/Folder.png" />
                        <TextBlock Text="{Binding Path=Name, Mode=TwoWay}" />
                    </StackPanel>
                </MenuItem.Header>
                <ItemsControl ItemsSource="{Binding Path=Groups}" />
                <ItemsControl ItemsSource="{Binding Path=Files}" />
            </MenuItem>
        </HierarchicalDataTemplate>
    </Menu.Resources>
</Menu>

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开柳相依 2024-11-07 22:01:05

我并没有真正看你做错了什么,我会发布有效的代码。

<Menu ItemsSource="{Binding Menus}" Name="menu" Height="20">        
    <Menu.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Items}" DataType="{x:Type plugin:MenuEntry}">
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{Binding Command}" />
                    <Setter Property="Icon" Value="{Binding Icon}" />
                    <Setter Property="InputGestureText" Value="{Binding InputGestureText}" />
                    <Setter Property="IsCheckable" Value="{Binding IsCheckable}" />
                    <Setter Property="IsChecked" Value="{Binding IsChecked}" />

                    <!-- Handle separators -->
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Header}" Value="[Separator]">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Separator />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>                    
            </HierarchicalDataTemplate.ItemContainerStyle>
            <ContentControl Content="{Binding Header}"/>                
        </HierarchicalDataTemplate>
    </Menu.ItemTemplate>
</Menu>

菜单绑定到简单的对象集合,这些对象具有标题、图标等所有属性。它们还有一个包含子项目的 Items 属性。

这并不完全是您想要的,但也许您能够解决它。我记得尝试过像你一样的类似方法,但我记得失败得很严重。菜单生成它自己的 MenuItem 对象(并且您试图在其中添加另一个对象 - 您不能这样做,您必须只对其进行样式化),而且两个 ItemsControl 对我来说看起来有点奇怪(尽管我理解你需要它们)。

I didn't really look what you did wrong, I will post code that works instead.

<Menu ItemsSource="{Binding Menus}" Name="menu" Height="20">        
    <Menu.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Items}" DataType="{x:Type plugin:MenuEntry}">
            <HierarchicalDataTemplate.ItemContainerStyle>
                <Style TargetType="MenuItem">
                    <Setter Property="Command" Value="{Binding Command}" />
                    <Setter Property="Icon" Value="{Binding Icon}" />
                    <Setter Property="InputGestureText" Value="{Binding InputGestureText}" />
                    <Setter Property="IsCheckable" Value="{Binding IsCheckable}" />
                    <Setter Property="IsChecked" Value="{Binding IsChecked}" />

                    <!-- Handle separators -->
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Header}" Value="[Separator]">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <Separator />
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>                    
            </HierarchicalDataTemplate.ItemContainerStyle>
            <ContentControl Content="{Binding Header}"/>                
        </HierarchicalDataTemplate>
    </Menu.ItemTemplate>
</Menu>

Menus is bound to simple collection of objects which have all the properties like Header, Icon etc. They also have a Items property which contains the sub-items.

It is not entirely what are you looking for, but perhaps you will be able to fix it. I remember trying similar approach like you did and I remember failing badly. The Menu generates the MenuItem object in its own (and you are trying to add another one in it - you must not do that, you have to only style it instead), also the two ItemsControls look a little weird to me (though I understand you need them).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文