WPF中的選單模板
阿新 • • 發佈:2018-11-07
原文:
WPF中的選單模板
資源字典程式碼如下: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <!--Menu控制元件模板--> <ControlTemplate x:Key="mycontroltemplate" TargetType="{x:Type Menu}"> <Border Margin="2" Background="Chocolate" CornerRadius="3" SnapsToDevicePixels="True"> <ItemsPresenter/> </Border> </ControlTemplate> <!--MenuItem控制元件模板--> <ControlTemplate x:Key="mymenuitemtemplate" TargetType="MenuItem"> <Border Name="Border"> <Grid> <ContentPresenter Margin="10" ContentSource="Header" RecognizesAccessKey="True"/> <Popup AllowsTransparency="True" Name="Popup" Placement="Top" IsOpen="{TemplateBinding IsSubmenuOpen}" Focusable="False" PopupAnimation="Slide"> <Border CornerRadius="30" Name="SubmenuBorder" SnapsToDevicePixels="True"> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" /> <Border.Background> <DrawingBrush> <DrawingBrush.Drawing> <GeometryDrawing Brush="YellowGreen"> <GeometryDrawing.Geometry> <CombinedGeometry GeometryCombineMode="Exclude"> <CombinedGeometry.Geometry1> <EllipseGeometry RadiusX="20" RadiusY="20"/> </CombinedGeometry.Geometry1> <CombinedGeometry.Geometry2> <EllipseGeometry RadiusX="10" RadiusY="10"/> </CombinedGeometry.Geometry2> </CombinedGeometry> </GeometryDrawing.Geometry> </GeometryDrawing> </DrawingBrush.Drawing> </DrawingBrush> </Border.Background> </Border> </Popup> </Grid> </Border> <!--可有可無________________________________________________________________________________--> <!--<ControlTemplate.Triggers> <Trigger Property="IsSuspendingPopupAnimation" Value="true"> <Setter TargetName="Popup" Property="PopupAnimation" Value="None"/> </Trigger> <Trigger Property="IsHighlighted" Value="true"> <Setter TargetName="Border" Property="Background" Value="Transparent"/> <Setter TargetName="Border" Property="BorderBrush" Value="Transparent"/> </Trigger> <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="True"> <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4"/> <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="Black"/> </Trigger> </ControlTemplate.Triggers>--> <!--可有可無________________________________________________________________________________--> </ControlTemplate> </ResourceDictionary> 窗體XAML程式碼如下: <Window x:Class="WPF中的選單模板.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/MyDictionary/MyDictionary.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources> <DockPanel> <Menu DockPanel.Dock="Bottom" Template="{StaticResource mycontroltemplate}"> <MenuItem Header="開始"> <MenuItem Header="開始"></MenuItem> <MenuItem Header="開始"></MenuItem> <MenuItem Header="開始"></MenuItem> <MenuItem Header="開始"></MenuItem> </MenuItem> <Menu.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}"> <Setter Property="Template" Value="{StaticResource mymenuitemtemplate}"/> </Style> </Menu.ItemContainerStyle> </Menu> <Popup></Popup> </DockPanel> </Window>