首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WFP应用程序中重用一个按钮

在WFP应用程序中重用一个按钮
EN

Stack Overflow用户
提问于 2014-02-21 18:08:22
回答 1查看 420关注 0票数 0

我正在开发我的第一个win8应用程序。我需要在几页上创建一个注销按钮。一般来说,这是一个按钮,里面有一些文字和图像。

我应该选择什么方法:装入模板、资源或样式?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-21 18:28:43

您可以使用style重用按钮。样式应该定义它的template,而resource只是基于定义它的位置而可用。

针对您用x:Key定义的按钮的样式示例,以便在其他地方重用。如果您没有指定x:Key,并且style是在Application Resources中定义的,那么它将将样式应用于所有按钮。

代码语言:javascript
复制
<Style x:Key="CustomStyleButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#373737" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="15" />
<Setter Property="SnapsToDevicePixels" Value="True" />

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border CornerRadius="4" Background="{TemplateBinding Background}">
                <Grid>
                    <Path x:Name="PathIcon" Width="15" Height="25" Stretch="Fill" Fill="#4C87B3" HorizontalAlignment="Left" Margin="17,0,0,0" Data="F1 M 30.0833,22.1667L 50.6665,37.6043L 50.6665,38.7918L 30.0833,53.8333L 30.0833,22.1667 Z "/>
                    <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" />                                
                </Grid>
            </Border>

            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#E59400" />
                    <Setter Property="Foreground" Value="White" />
                    <Setter TargetName="PathIcon" Property="Fill" Value="Black" />
                </Trigger>

                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="OrangeRed" />
                    <Setter Property="Foreground" Value="White" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Setter.Value>
</Setter>

样本使用

代码语言:javascript
复制
<Button Width="200" Height="50" Style="{StaticResource CustomStyleButton}" VerticalAlignment="Top" Margin="0,20,0,0" />
<Button.Content>
    <StackPanel>
        <TextBlock Text="Watch Now" FontSize="20" />
        <TextBlock Text="Duration: 50m" FontSize="12" Foreground="Gainsboro" />
    </StackPanel>
</Button.Content>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21941964

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档