wpf 自定义ContextMenu样式,可以调节ContextMenu的宽度

 之前在网上找的ContextMenu的样式,都不可以把宽度设置到142以下,否则文字就被隐藏了一部分,所以重新改了下样式,下面是xaml代码,注意动画的最终的高宽和MenuItem的高宽,改下看效果就知道实现自己的效果要改那些东西了.


 

<Style TargetType="{x:Type ContextMenu}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContextMenu}">
                        <Border x:Name="Border" CornerRadius="4" Background="#A5181818"  BorderThickness="2">
                            <Border.BorderBrush>
                                <SolidColorBrush Color="#BABABA" />
                            </Border.BorderBrush>
                            <ItemsPresenter/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"  Storyboard.TargetProperty="(FrameworkElement.Width)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00.0020000" Value="0"/>
                                                <SplineDoubleKeyFrame KeyTime="00:00:00.3450000" Value="95"/>
                                            </DoubleAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00"  Storyboard.TargetProperty="(FrameworkElement.Height)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00.0020000" Value="0"/>
                                                <SplineDoubleKeyFrame KeyTime="00:00:00.3450000" Value="54"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </MultiTrigger.EnterActions>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="{x:Type MenuItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <Border Margin="1" MinWidth="80" CornerRadius="5">
                            <Border.Background>
                                <SolidColorBrush x:Name="MyAnimatedBrushBackground" Color="Transparent" />
                            </Border.Background>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text=" "/>
                                <ContentPresenter ContentSource="Icon" HorizontalAlignment="Center" VerticalAlignment="Center" />
                                <TextBlock Text="   "/>  
                                <ContentPresenter ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </StackPanel>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="MyAnimatedBrushBackground" Storyboard.TargetProperty="Color">
                                                <LinearColorKeyFrame Value="Transparent" KeyTime="00:00:00.0020000" />
                                                <LinearColorKeyFrame Value="#7E9C9C9C" KeyTime="00:00:00.0320000" />
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </MultiTrigger.EnterActions>
                                <MultiTrigger.ExitActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="MyAnimatedBrushBackground" Storyboard.TargetProperty="Color">
                                                <LinearColorKeyFrame Value="#7E9C9C9C" KeyTime="00:00:00.0020000" />
                                                <LinearColorKeyFrame Value="Transparent" KeyTime="00:00:00.0320000" />
                                            </ColorAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </BeginStoryboard>
                                </MultiTrigger.ExitActions>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 

< ContextMenu  Cursor ="Hand"  StaysOpen ="true" >
                 < MenuItem  Name ="menuLook"  Foreground ="Black"  Height ="25" >
                     < MenuItem.Icon >
                         < Image   Width ="21"  Height ="21"  Source ="search.png" ></ Image >
                     </ MenuItem.Icon >
                     < MenuItem.Header >
                         < TextBlock  Text ="查看图片"  Height ="21" />
                     </ MenuItem.Header >
                 </ MenuItem >
                 < MenuItem  Name ="menuEdit"  Foreground ="Black"  Height ="25" >
                     < MenuItem.Icon >
                         < Image  Width ="21"  Height ="21"  Source ="tools.png" ></ Image >
                     </ MenuItem.Icon >
                     < MenuItem.Header >
                         < TextBlock  Text ="图片增强"  Height ="21" />
                     </ MenuItem.Header >
                 </ MenuItem >

            </ContextMenu> 

你可能感兴趣的:(contextMenu)