ad

自定义内涵菜单内容-英雄云拓展知识分享

匿名投稿 416 2024-01-21

我正在尝试构造自定义内涵菜单(这意味着我想设置菜单项的模板)

<Window x:Class="WpfApp3.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

自定义内涵菜单内容-英雄云拓展知识分享

xmlns:local="clr-namespace:WpfApp3"

mc:Ignorable="d"

Title="MainWindow" Height="350" Width="525"

DataContext="{Binding Source={StaticResource Locator}, Path=Main}">

<Window.Resources>

<ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">

<Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">

<Grid VerticalAlignment="Center">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="Auto"/>

<ColumnDefinition Width="Auto"/>

</Grid.ColumnDefinitions>

<ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>

<Path x:Name="GlyphPanel" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="#FF212121" FlowDirection="LeftToRight" Margin="3" Visibility="Collapsed" VerticalAlignment="Center"/>

<ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="1" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

</Grid>

</Border>

<ControlTemplate.Triggers>

<Trigger Property="Icon" Value="{x:Null}">

<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>

</Trigger>

<Trigger Property="IsChecked" Value="True">

<Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>

<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>

</Trigger>

<Trigger Property="IsHighlighted" Value="True">

<Setter Property="Background" TargetName="templateRoot" Value="#3D26A0DA"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>

</Trigger>

<Trigger Property="IsEnabled" Value="False">

<Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>

<Setter Property="Fill" TargetName="GlyphPanel" Value="#FF707070"/>

</Trigger>

<MultiTrigger>

<MultiTrigger.Conditions>

<Condition Property="IsHighlighted" Value="True"/>

<Condition Property="IsEnabled" Value="False"/>

</MultiTrigger.Conditions>

<Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/>

<Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/>

</MultiTrigger>

</ControlTemplate.Triggers>

</ControlTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}"

ItemsSource="{Binding MenuItems}"

>

<TextBlock Text="{Binding Caption}" />

</HierarchicalDataTemplate>

</Window.Resources>

<Grid>

<Menu ItemsSource="{Binding MenuItems}"

>

<Menu.ItemContainerStyle>

<Style TargetType="{x:Type MenuItem}">

<Setter Property="Padding" Value="0" />

<Setter Property="HorizontalContentAlignment" Value="Stretch" />

<Setter Property="HorizontalAlignment" Value="Stretch" />

<Setter Property="VerticalContentAlignment" Value="Center" />

<Setter Property="BorderThickness" Value="0" />

<Setter Property="Template" Value="{StaticResource MenuItemControlTemplate1}" />

</Style>

</Menu.ItemContainerStyle>

</Menu>

</Grid>

</Window>

和视野模型

public class MainViewModel : ViewModelBase

{

public ObservableCollection<MenuItemViewModel> MenuItems { get; set; }

public MainViewModel()

{

MenuItems = new ObservableCollection<MenuItemViewModel>

{

new MenuItemViewModel("File")

{

MenuItems = new ObservableCollection<MenuItemViewModel>

{

new MenuItemViewModel("New"),

new MenuItemViewModel("Open"),

new MenuItemViewModel("Close"),

}

},

new MenuItemViewModel("Edit")

{

MenuItems = new ObservableCollection<MenuItemViewModel>

{

new MenuItemViewModel("Cut"),

new MenuItemViewModel("Copy"),

new MenuItemViewModel("Paste"),

}

},

};

}

}

问题是我看到了第一个层次结构 - “文件”和“编辑”,但没有他们的孩子...

在史努比,我可以看到他们有孩子。

如果我删除这一行:

<Setter Property="Template" Value="{StaticResource MenuItemControlTemplate1}" />

它也能够正常工作...问题是我确切需要为菜单项设置模板...

看答案

您的自定义内涵模板没有任何内容可以显示子项目。每当您想创建自定义内涵控制模板时,都应查找 示例模板 首先要确保您不会错过任何重要的事情。

在里面 示例模板,您会注意到它包括以下内容:

<StackPanel

IsItemsHost="True"

KeyboardNavigation.DirectionalNavigation="Cycle" />

任何 ItemsControl (menuitem扩大)需要一个 Panel 元素充当 ItemsHost。因此,这种情况下,在模板中的某个地方添加此元素。

如果所有其他方法都失败了,请尝试使用示例模板准确地按原样使用。然后,您可使用它来更好地理解它。


🚀🌟 点击注册 免费试用超级应用平台-英雄云企业级hpapaas 🌟🚀 😃👉🌐

免责声明:

本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。

标签:C# WPF
上一篇:带有GZIP紧缩的Protobuf-英雄云拓展知识分享
下一篇:Alexa技能套件:询问用户意图中间,然后针对用户响应在同一意图的范围内继续进行?-英雄云拓展知识分享
相关文章
×