如何在ControlMmplate中为IsMouseOver触发器设置Button的内容

时间:2014-03-14 13:38:00

标签: wpf button triggers controltemplate

我正在更改Button的ControlTemplate并且我成功地能够更改视觉外观,但是当我尝试在ControlTemplate内部更改IsMouseOver触发器上的按钮内容时,它不起作用。以下是我的xaml:

<Window x:Class="WpfApplication1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">

  <Window.Resources>        

    <ControlTemplate x:Key="ngButton" TargetType="Button">
        <Grid>
            <Border Name="templateBorder" BorderThickness="2" Background="Blue" />


            <Ellipse  Name="el2" Margin="10,10,10,10" Fill="Magenta" />

            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"/>
        </Grid>


        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="templateBorder" Property="Background" Value="DarkRed" />
                <Setter TargetName="el2" Property="Fill" Value="Green" />
                <Setter Property="Content">
                    <Setter.Value>
                            <TextBlock Text="Mouse is over" />
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
                <Setter TargetName="templateBorder" Property="Background" Value="LightBlue" />
                <Setter TargetName="templateBorder" Property="BorderBrush" Value="DarkKhaki" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>


</Window.Resources>

<Grid>

    <Button Content="Hello World" Template="{StaticResource ngButton}" Name="ngButtonHello" Height="200" Width="200"/>

</Grid> 

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

请阅读Dependency Property Value Precedence:本地值(&#34; Hello World&#34;在您的示例中)始终优先于触发器设置的值。

在样式中设置原始内容属性,或使用本地值属性中的转换器将IsMouseOver转换为特定字符串。