将ControlTemplateTrigger绑定到TextBlock属性

时间:2015-01-19 00:33:03

标签: c# wpf button triggers controltemplate

每当TextBlock突出显示时,我都会尝试更改Button文字颜色。

但是,我不知道如何将ControlTemplate.Trigger绑定到TextBlock Foreground。我尝试使用TextBlock Name,然后在TargetName中使用Setter,但它说该名称未被识别。

<Button Name="Home" HorizontalAlignment="Left" Width="75" Click="Button_Click_Home" Background="#FF252525" BorderThickness="5">
<Button.Content>
    <Grid HorizontalAlignment="Center" VerticalAlignment="Bottom">
        <TextBlock  FontFamily="/VideoManager;component/#Myriad Pro" FontSize="13.333" Foreground="White" Text="Home"></TextBlock>
    </Grid>
</Button.Content>
<Button.Template>
    <ControlTemplate TargetType="{x:Type Button}">
        <ContentPresenter />
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="#FF360A0A" /> // What to put here..
            </Trigger>
        </ControlTemplate.Triggers> 
    </ControlTemplate>
</Button.Template>

1 个答案:

答案 0 :(得分:1)

这对你有所帮助,但它有很多损失,所以我建议你阅读更多关于样式和模板的信息

<Button Name="Home" HorizontalAlignment="Left" Width="75" Background="#FF252525" BorderThickness="5">
    <Button.Content>
        <Grid HorizontalAlignment="Center" VerticalAlignment="Bottom">
            <TextBlock  FontFamily="/VideoManager;component/#Myriad Pro" FontSize="13.333" Text="Home"></TextBlock>
        </Grid>
    </Button.Content>
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border Background="{TemplateBinding Background}">
                <ContentPresenter />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Yellow" />
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>