如何使用触发器更改TextBlock的前景色?

时间:2012-12-17 13:17:13

标签: wpf textblock datatrigger foreground

我想从dependencyproperty更改TextBlock的前景色 但我不改变文本块颜色 我的代码中不知道这个问题 如何使用触发器更改TextBlock的前景色?

XAML:

<TextBlock Name="TestBlock" Text="Test color" >
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <Trigger Property ="IsMouseOver" Value="True">
                    <Setter Property= "Foreground" Value="Gray"/>
                </Trigger>
                <DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="0">
                    <Setter Property="Foreground" Value="Red" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="1">
                    <Setter Property="Foreground" Value="Blue" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="2">
                    <Setter Property="Foreground" Value="Green" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=TestColorMode2, RelativeSource={RelativeSource AncestorType={x:Type local:Window1}}}" Value="3">
                    <Setter Property="Foreground" Value="Black" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

CODE:

public static DependencyProperty TestColorModeProperty = DependencyProperty.Register("TestColorMode", typeof(int), typeof(UpdateProgressItem));

public int TestColorMode
{
    get { return (int)GetValue(TestColorModeProperty); }
    set { SetValue(TestColorModeProperty, value); }
}

            ....

private void button1_Click(object sender, RoutedEventArgs e)
{
    TestColorMode++;
}

2 个答案:

答案 0 :(得分:6)

也指定类型:

<Trigger Property="TextBlock.IsMouseOver" Value="true">
     <Setter Property="TextBlock.Foreground" Value="Gray" />
</Trigger>

答案 1 :(得分:3)

这种方式可以帮助你:

<Style x:Key="SimpleLabel" TargetType="{x:Type Label}">
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Grid>
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/>
                        <Setter Property="Foreground" Value="{DynamicResource DisabledForegroundBrush}"/>
                    </Trigger>
                    <Trigger Property="TextBlock.IsMouseOver" Value="true">
                        <Setter Property="TextBlock.Foreground" Value="Gray" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>