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}"/>
</Stac开发者_如何转开发kPanel>
</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?
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).
精彩评论