将 WPF TreeView 转换为菜单
我在将此工作树视图转换为菜单时遇到问题。
该树视图显示正确。
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我并没有真正看你做错了什么,我会发布有效的代码。
菜单绑定到简单的对象集合,这些对象具有标题、图标等所有属性。它们还有一个包含子项目的 Items 属性。
这并不完全是您想要的,但也许您能够解决它。我记得尝试过像你一样的类似方法,但我记得失败得很严重。菜单生成它自己的 MenuItem 对象(并且您试图在其中添加另一个对象 - 您不能这样做,您必须只对其进行样式化),而且两个 ItemsControl 对我来说看起来有点奇怪(尽管我理解你需要它们)。
I didn't really look what you did wrong, I will post code that works instead.
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).