Originally i tried to change color of pen in TextDecoration by trigger, but when i set x:Name property for pen i have got an error that target not found, so i had changed my template to following:
<ControlTemplate x:Key="TabButton" TargetType="{x:Type Button}">
<Border x:Name="border" Padding="10,5" CornerRadius="5,5,0,0" Background="#62A9FF">
<TextBlock x:Name="text" Text="{TemplateBinding Content}"
Cursor="Hand" Foreground="#F9FCFF">
<TextBlock.TextDecorations>
<TextDecoration Location="Underline"
PenThicknessUnit="FontRecommended">
<TextDecoration.Pen>
<Pen Brush="White" Thickness="1">
<Pen.DashStyle>
<DashStyle Dashes="5"/>
</Pen.DashStyle>
</Pen>
</TextDecoration.Pen>
</TextDecoration>
</TextBlock.TextDecorations>
</TextBlock>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="local:MainWindow.TabActive" Value="False">
<Setter TargetName="border" Property="Background" Value="Transparent"/>
<Setter TargetName="text" Property="Foreground" Value="Black"/>
<Setter TargetName="text" Property="TextDecorations">
<Setter.Value>
<TextDecorationCollection>
<TextDecoration Location="Underline">
<TextDecoration.Pen>
<Pen Brush="Gray" Thickness="1">
<Pen.DashStyle>
<DashStyle Dashes="5"/>
</Pen.DashStyle>
</Pen>
</TextDecoration.Pen>
</TextDecoration>
</TextDecorationCollection>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Now application builds without error but when i run it i get error:
Cannot add content of type 'System.Windows.TextDecoration' to an object of typ开发者_StackOverflowe System.Windows.TextDecorationCollection
What is wrong? Or may be there is another way?
Cannot reproduce an error like that. Note that your triggers as they are would not work even if there was no error since you directly set the values on the target objects which take precedence over the trigger-setters.
Here is an example of variable TextDecorations
which works, changes on mouse-over:
<ContentControl Content="Test">
<ContentControl.Template>
<ControlTemplate TargetType="{x:Type ContentControl}">
<TextBlock Name="tb" Text="{TemplateBinding Content}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextDecorations">
<Setter.Value>
<TextDecorationCollection>
<TextDecoration>
<TextDecoration.Pen>
<Pen Brush="Blue" />
</TextDecoration.Pen>
</TextDecoration>
</TextDecorationCollection>
</Setter.Value>
</Setter>
</Style>
</TextBlock.Style>
</TextBlock>
<ControlTemplate.Triggers>
<Trigger Property="ContentControl.IsMouseOver" Value="True">
<Setter TargetName="tb" Property="TextDecorations">
<Setter.Value>
<TextDecorationCollection>
<TextDecoration>
<TextDecoration.Pen>
<Pen Brush="Red" />
</TextDecoration.Pen>
</TextDecoration>
</TextDecorationCollection>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
Mine is a similar approach but without the templated control.
<TextBlock Text="5000.00" FontSize="20" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property="TextDecorations">
<Setter.Value>
<TextDecorationCollection>
<TextDecoration Location="Underline">
<TextDecoration.Pen>
<Pen Brush="Red"/>
</TextDecoration.Pen>
</TextDecoration>
</TextDecorationCollection>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property ="IsMouseOver" Value="False">
<Setter Property="TextDecorations">
<Setter.Value>
<TextDecorationCollection>
<TextDecoration Location="Underline">
<TextDecoration.Pen>
<Pen Brush="LimeGreen"/>
</TextDecoration.Pen>
</TextDecoration>
</TextDecorationCollection>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
精彩评论