您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何在WPF中自定義MenuItem樣式,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
MenuItem的樣式代碼:
<!--MenuItem--> <Style TargetType="MenuItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="MenuItem"> <Grid ToolTip="{TemplateBinding Header}" Height="24" Background="#FBFBFD" Width="{TemplateBinding Width}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="30" /> <ColumnDefinition Width="*" MinWidth="80"/> </Grid.ColumnDefinitions> <Border Background="#EBEBED"/> <Border x:Name="_border" Grid.ColumnSpan="2"/> <Image x:Name="_imgicon" Width="12" Height="12" Stretch="Uniform" Source="{Binding Icon,RelativeSource={RelativeSource TemplatedParent}}" /> <TextBlock Foreground="#2D2D30" Grid.Column="1" Margin="5 0 15 0" VerticalAlignment="Center" x:Name="_txt" Text="{Binding Header,RelativeSource={RelativeSource TemplatedParent}}" /> <Path Visibility="Collapsed" x:Name="_path" Grid.Column="1" HorizontalAlignment="Right" Margin="5 13 5 0" Data="M0,4 4,0 0,-4 z" Fill="#7E8A92"/> <Popup Placement="Right" x:Name="SubMenuPopup" AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsSubmenuOpen}" > <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <Border Background="#ffc787" Name="SubmenuBorder" BorderBrush="LightGray" BorderThickness="1" SnapsToDevicePixels="True" > <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" /> </Border> </ScrollViewer> </Popup> </Grid> <ControlTemplate.Triggers> <Trigger Property="HasItems" Value="true"> <Setter TargetName="_path" Property="Visibility" Value="Visible"/> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Setter TargetName="_border" Property="Background" Value="#ffc787"/> <Setter TargetName="_txt" Property="Foreground" Value="White"/> <Setter TargetName="_path" Property="Fill" Value="White"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter TargetName="_txt" Property="Foreground" Value="Gray"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
引用示例:
<Grid> <Grid.ContextMenu> <ContextMenu> <MenuItem Header="菜單一"> <MenuItem Header="子菜單"/> </MenuItem> <MenuItem Header="菜單二"></MenuItem> </ContextMenu> </Grid.ContextMenu> </Grid>
關于如何在WPF中自定義MenuItem樣式就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。