How can I change the Foreground color of a TextBlock with a Trigger?

♀尐吖头ヾ 提交于 2019-12-10 15:52:36

问题


I want to change foreground color of a TextBlock from dependencyproperty.
But I don't change textblock color.
I don't know this problem in my code.
How can I change the foreground color of a TextBlock with a Trigger?

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++;
}

回答1:


This Way will help you:

<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>



回答2:


Specify the type too:

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


来源:https://stackoverflow.com/questions/13914943/how-can-i-change-the-foreground-color-of-a-textblock-with-a-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!