简体   繁体   English

在按钮内一起定义模板和样式

[英]Defining template and style together within the button

I have WPF button below: 我在下面有WPF按钮:

<Button Name="btnAdd" Click="btnAdd_Click">                    
    <Button.Template>
        <ControlTemplate>                          
            <StackPanel Orientation="Vertical">
                <Image Height="20" Width="20" Stretch="UniformToFill" Source="./Resources/Add.png"/>                               
                <Label HorizontalAlignment="Center">Add</Label>
            </StackPanel>
        </ControlTemplate>                        
    </Button.Template>                     
</Button>

Now within the wpf button I am trying to add the following style, I mean, combining style with template in the button: 现在在wpf按钮中,我试图添加以下样式,我的意思是,将样式与模板中的样式结合起来:

<Button>
    <Button.Style>
        <Style TargetType="Button" BasedOn="{StaticResource MyStyle}">
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Foreground" Value="Green" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

How can I do this? 我怎样才能做到这一点?

One way could be adding a Setter to your Style for Template property . 一种方法是在您的Style for Template 属性中添加一个Setter Like this: 像这样:

<Button.Style>
    <Style TargetType="Button" BasedOn="{StaticResource MyStyle}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel Orientation="Vertical">
                        <Image Height="20" Width="20" Stretch="UniformToFill" Source="./Resources/Add.png"/>
                        <Label HorizontalAlignment="Center">Add</Label>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Foreground" Value="Green" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Button.Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM