【问题标题】:How to set Content for a Button inside of ControlTemplate for IsMouseOver Trigger如何为 IsMouseOver 触发器的 ControlTemplate 内的按钮设置内容
【发布时间】:2014-04-19 20:06:36
【问题描述】:

我正在更改我的 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> 

感谢您的帮助。

【问题讨论】:

    标签: wpf button triggers controltemplate


    【解决方案1】:

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

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

    【讨论】:

    • 太棒了。感谢您的帮助。
    猜你喜欢
    • 2014-11-04
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多