如何获取 TreeViewItem 的 ContextMenu,将 TreeView 作为 DataTemplate
How to get TreeViewItem's ContextMenu, having TreeView as a DataTemplate
我在这样的数据模板中有一个 TreeView :
<DataTemplate DataType="{x:Type MainFavoriteItems}">
<TreeView BorderThickness="0">
<TreeViewItem Header="First Header" ItemsSource="{Binding FavItems}" ItemTemplate="{StaticResource item_template}"/>
<TreeViewItem Header="Second Header" ../>
<TreeViewItem Header="Third Header" ../>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="ContextMenu" Value="{Binding GetMenu}}"/> // DO not work
</TreeView.ItemContainerStyle>
</TreeView>
</DataTemplate>
internal class MainFavoriteItems{
public IReadOnlyList<Info> FavItems
{
get
{
List<Info> items = new List<Info>();
items.Add(new InnerViewModel("1"));
items.Add(new InnerViewModel("2"));
NotifyPropertyChanged(() => FavItems);
return items;
}
}
}
public class InnerViewModel
{
public string Id;
public System.Windows.Controls.ContextMenu GetMenu => new CustomContextMenu();
...
}
当我 运行 它时,headers 和它的 children 看起来很好,但是内部项目的 right-click 上下文菜单没有显示,它的 xaml输出错误说 .. object 'MainFavoriteItems' 上找不到 GetMenu 属性 .. 我做错了什么!?谢谢
您的示例代码甚至无法编译,但您应该将 TreeViewItem
的 ItemContainerStyle
设置为 ItemsSource
以使绑定生效:
<TreeViewItem Header="First Header" ItemsSource="{Binding FavItems}"
ItemTemplate="{StaticResource item_template}">
<TreeViewItem.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="ContextMenu" Value="{Binding GetMenu}"/>
</Style>
</TreeViewItem.ItemContainerStyle>
</TreeViewItem>
<TreeViewItem Header="Second Header" />
<TreeViewItem Header="Third Header" />
我在这样的数据模板中有一个 TreeView :
<DataTemplate DataType="{x:Type MainFavoriteItems}">
<TreeView BorderThickness="0">
<TreeViewItem Header="First Header" ItemsSource="{Binding FavItems}" ItemTemplate="{StaticResource item_template}"/>
<TreeViewItem Header="Second Header" ../>
<TreeViewItem Header="Third Header" ../>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="ContextMenu" Value="{Binding GetMenu}}"/> // DO not work
</TreeView.ItemContainerStyle>
</TreeView>
</DataTemplate>
internal class MainFavoriteItems{
public IReadOnlyList<Info> FavItems
{
get
{
List<Info> items = new List<Info>();
items.Add(new InnerViewModel("1"));
items.Add(new InnerViewModel("2"));
NotifyPropertyChanged(() => FavItems);
return items;
}
}
}
public class InnerViewModel
{
public string Id;
public System.Windows.Controls.ContextMenu GetMenu => new CustomContextMenu();
...
}
当我 运行 它时,headers 和它的 children 看起来很好,但是内部项目的 right-click 上下文菜单没有显示,它的 xaml输出错误说 .. object 'MainFavoriteItems' 上找不到 GetMenu 属性 .. 我做错了什么!?谢谢
您的示例代码甚至无法编译,但您应该将 TreeViewItem
的 ItemContainerStyle
设置为 ItemsSource
以使绑定生效:
<TreeViewItem Header="First Header" ItemsSource="{Binding FavItems}"
ItemTemplate="{StaticResource item_template}">
<TreeViewItem.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}">
<Setter Property="ContextMenu" Value="{Binding GetMenu}"/>
</Style>
</TreeViewItem.ItemContainerStyle>
</TreeViewItem>
<TreeViewItem Header="Second Header" />
<TreeViewItem Header="Third Header" />