惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
Engineering at Meta
Engineering at Meta
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
J
Java Code Geeks
Recent Announcements
Recent Announcements
Jina AI
Jina AI
G
Google Developers Blog
腾讯CDC
博客园_首页
博客园 - 【当耐特】

博客园 - 昨夜飘风

WPF 通过共享内存播放视频 C# 模拟网页拖拽 .net4.0中wpf单例启动 Oracle 开发经验 WPF跑马灯效果 android私有文件夹的访问 [转]C#内存操作 C#串口发送接受数据 C# 制作外挂常用的API silverlight 动画 性能优化 SilverLight 动画缓冲(Animation Easing) - 昨夜飘风 体面地处理程序的未捕获异常 C# 访问USB(HID)设备 收藏一个地址(居然说我标题已经存在) 收藏一个地址 (2009-06-24) SqlCommandBuilder使用的注意事项 助睡眠音乐 (2009-06-22) 班德瑞官方试听网站 (2009-06-18)今日收获 工厂方法 (2009-06-17)今日收获-只读列隐藏-win2003 dll文件下载
wpf 自定义ContextMenu样式,可以调节ContextMenu的宽度
昨夜飘风 · 2012-06-20 · via 博客园 - 昨夜飘风

 之前在网上找的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>