WPF通过ControlTemplate

时间:2016-04-22 15:19:17

标签: wpf button triggers controltemplate

Button ControlTemplate我需要访问IsPressed事件。我该怎么做?

背景:我向DropShadowEffect Button添加了一个Style,但文字模糊。 This fix solved the blurry text issue.然而,这增加了一个新问题。我想在用户点击按钮时删除DropShadowEffect。想我会这样做;

<Style x:Key="DropShadowButtons" TargetType="Button">
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Border Background="{TemplateBinding Background}">
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="Border.Effect">
                                <Setter.Value>
                                    <DropShadowEffect Color="Black" Opacity="0.5" />
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <Trigger Property="Button.IsPressed" Value="False">
                                    <Setter Property="Border.Effect">
                                        <Setter.Value>
                                            <DropShadowEffect Color="Black" Direction="320" ShadowDepth="0" BlurRadius="0" Opacity="0" />
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                </Border>
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="5,2,5,0"/>
                </Border>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

然后发现Button.IsPressed不会成为Border的一部分。那么ControlTemplate如何访问Button的事件IsPressed?

1 个答案:

答案 0 :(得分:0)

您只需要在ControlTemplate上使用触发器而不是Border的样式。要让Setter定位Border,你需要给Border一个名字,你需要给Setter一个TargetName:

<ControlTemplate TargetType="{x:Type Button}">
    <Grid>
        <Border x:Name="BackgroundBorder" Background="{TemplateBinding Background}">
            <Border.Style>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="Border.Effect">
                        <Setter.Value>
                            <DropShadowEffect Color="Black" Opacity="0.5" />
                        </Setter.Value>
                    </Setter>
                </Style>
            </Border.Style>
        </Border>
        <Border Background="{TemplateBinding Background}">
            <ContentPresenter
                HorizontalAlignment="Center" 
                VerticalAlignment="Center" 
                Margin="5,2,5,0"
                />
        </Border>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Setter TargetName="BackgroundBorder" Property="Effect">
                <Setter.Value>
                    <DropShadowEffect 
                        Color="Black" 
                        Direction="320" 
                        ShadowDepth="0" 
                        BlurRadius="0" 
                        Opacity="0" 
                        />
                </Setter.Value>
            </Setter>
        </Trigger>
    </ControlTemplate.Triggers>        
</ControlTemplate>