具有相似样式 WPF 的 2 个按钮的可见性 [英] visibility of 2 buttons with similar style WPF

查看:29
本文介绍了具有相似样式 WPF 的 2 个按钮的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用具有以下样式的自定义样式 WPF 按钮 [目的是按钮内容(星形图像)仅在鼠标悬停时可见)

I use a custom style WPF button with following style [ Purpose is button content (a star image ) is visible on mouseover only)

   <Style x:Key="ViewBookmarkRemoveButtonStyle" TargetType="{x:Type Button}">
            <Style.Resources>
                <Storyboard x:Key="MouseOverAnimation" >
                    <DoubleAnimation Duration="0:0:1" To="1" Storyboard.TargetProperty="Opacity"/>
                </Storyboard>
                <Storyboard x:Key="MouseOutAnimation">
                    <DoubleAnimation Duration="0:0:1" To="0" Storyboard.TargetProperty="Opacity"/>
                </Storyboard>
            </Style.Resources>
            <Setter Property="Opacity" Value="1"></Setter>
            <Setter Property="Margin" Value="0"></Setter>
            <Setter Property="Content">
                <Setter.Value>
                    <Image  Source="images\star_inactive.png" Margin="15" />
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border Background="Transparent" >
                            <ContentPresenter Margin="{TemplateBinding Padding}"  HorizontalAlignment="Center" Content="{TemplateBinding Content}" VerticalAlignment="Center"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Transparent"/>
                    <Trigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource MouseOverAnimation}" />
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard Storyboard="{StaticResource MouseOutAnimation}" />
                    </Trigger.ExitActions>
                </Trigger>
            </Style.Triggers>
        </Style>

在页面 xaml 中,我使用了 2 个这样的按钮实例

And in page xaml i used 2 instances of the button like this

 <StackPanel>

                <Button Style="{StaticResource ResourceKey=ViewBookmarkAddButtonStyle}"
                                   Name="btnLeft" VerticalAlignment="Top" HorizontalAlignment="Right"                                  
                               ></Button>

                <Button Style="{StaticResource ResourceKey=ViewBookmarkAddButtonStyle}"
                                    Name="btnRight" VerticalAlignment="Top" HorizontalAlignment="Right"                                        
                                    ></Button>
            </StackPanel>

但是在运行时只有一个按钮是可见的.是否存在任何此类约束?或者我是否需要创建另一种样式的副本以使第二个按钮也遵循相同的样式?

But at run time only one button is visible. Is any such constarints exists or not? Or do i need to create a duplicate copy of one more style to make the second button also follow the same style?

推荐答案

x:Shared="False" 设置为样式

X:shared :设置为 false 时,修改 WPF 资源检索行为,以便对属性资源的请求为每个请求创建一个新实例,而不是为所有请求共享相同的实例.

X:shared : When set to false, modifies WPF resource-retrieval behavior so that requests for the attributed resource create a new instance for each request instead of sharing the same instance for all requests.

  <Style x:Key="ViewBookmarkRemoveButtonStyle" x:Shared="False"  TargetType="{x:Type Button}">

        ....

  </Style>

访问此链接以了解 x:共享

Visit this link for x:Shared

另一种方法:在 ContentPresenter.Content 中添加图片

Another approach : adding image in ContentPresenter.Content

   <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="Transparent" >
                        <ContentPresenter Margin="{TemplateBinding Padding}"  HorizontalAlignment="Center"  VerticalAlignment="Center">
                            <ContentPresenter.Content>
                                <Image Source="login.jpg" Margin="2"/>
                            </ContentPresenter.Content>
                        </ContentPresenter>
                    </Border>
    </ControlTemplate>

这篇关于具有相似样式 WPF 的 2 个按钮的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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