如何在WPF中为菜单应用样式 [英] How to apply a style for menu in wpf

查看:80
本文介绍了如何在WPF中为菜单应用样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我在sample.xaml中定义了样式.如何将样式应用于菜单.

Hi All

I have defined style in sample.xaml. How to apply that style to menu.

<Menu  Style="{DynamicResource RoundMenu}" Background="#FF7F90FC" BorderBrush="#95FFFFFF" Height="30" Name="menu1" Margin="10,10,344,0" VerticalAlignment="Top" HorizontalAlignment="Left"  BorderThickness="2"
              Foreground="White" FontSize="10" FontWeight="Bold" Width="700">



在这里,我尝试应用样式



Here i am trying to apply the style

RoundMenu

.但我无法申请.

. but i am not able to apply.

<Style x:Key="RoundMenu" TargetType="{x:Type Menu}">

       <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>

       <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>

       <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>

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

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

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

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

       <Setter Property="Template">

           <Setter.Value>

               <ControlTemplate TargetType="{x:Type Menu}">

                   <ControlTemplate.Resources>

                       <Storyboard x:Key="ShowShine">

                           <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Shine" Storyboard.TargetProperty="(UIElement.Opacity)">

                               <SplineDoubleKeyFrame KeyTime="00:00:00.5000000" Value="1"/>

                           </DoubleAnimationUsingKeyFrames>

                       </Storyboard>

                       <Storyboard x:Key="HideShine">

                           <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Shine" Storyboard.TargetProperty="(UIElement.Opacity)">

                               <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>

                           </DoubleAnimationUsingKeyFrames>

                       </Storyboard>

                   </ControlTemplate.Resources>

                   <Border CornerRadius="5,5,5,5" BorderThickness="1,1,1,1" RenderTransformOrigin="0.5,0.5" x:Name="border" BorderBrush="#FFFFFFFF">

                       <Border.RenderTransform>

                           <TransformGroup>

                               <ScaleTransform ScaleX="1" ScaleY="1"/>

                               <SkewTransform AngleX="0" AngleY="0"/>

                               <RotateTransform Angle="0"/>

                               <TranslateTransform X="0" Y="0"/>

                           </TransformGroup>

                       </Border.RenderTransform>

                       <Border Background="{TemplateBinding Background}" CornerRadius="5,5,5,5" x:Name="border1">

                           <Grid>

                               <Grid.RowDefinitions>

                                   <RowDefinition Height="0.5*"/>

                                   <RowDefinition Height="0.5*"/>

                               </Grid.RowDefinitions>

                               <Border Grid.Row="0" CornerRadius="5,5,0,0">

                                   <Border.Background>

                                       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">

                                           <GradientStop Color="#00FFFFFF" Offset="0"/>

                                           <GradientStop Color="#7EFFFFFF" Offset="1"/>

                                       </LinearGradientBrush>

                                   </Border.Background>

                               </Border>

                               <Border Grid.Row="1" Opacity="0" x:Name="Shine" Width="Auto" Height="Auto" CornerRadius="0,0,5,5" Margin="1,0,-1,0" Background="{TemplateBinding BorderBrush}"/>

                               <ContentPresenter VerticalAlignment="Center"  Grid.RowSpan="2" HorizontalAlignment="Center" x:Name="contentPresenter"/>

                           </Grid>

                       </Border>

                   </Border>

                   <ControlTemplate.Triggers>

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

                           <Setter Property="Opacity" TargetName="border1" Value="0.5"/>

                           <Setter Property="Opacity" TargetName="border" Value="1"/>

                           <Setter Property="Opacity" TargetName="contentPresenter" Value="0.5"/>

                       </Trigger>



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

                           <Trigger.ExitActions>

                               <BeginStoryboard Storyboard="{StaticResource HideShine}" x:Name="HideShine_BeginStoryboard"/>

                           </Trigger.ExitActions>

                           <Trigger.EnterActions>

                               <BeginStoryboard x:Name="ShowShine_BeginStoryboard" Storyboard="{StaticResource ShowShine}"/>

                           </Trigger.EnterActions>

                       </Trigger>

                   </ControlTemplate.Triggers>

               </ControlTemplate>

           </Setter.Value>

       </Setter>

   </Style>



谁能帮我


问候
Jesu



Can anyone help me


Regards
Jesu

推荐答案

尝试在您的样式标签中添加BasedOn ="{x:Type Menu}".
Try adding BasedOn="{x:Type Menu}" to your Style tag.


这篇关于如何在WPF中为菜单应用样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆