最初我试图通过触发器在TextDecoration中更改笔的颜色,但是当我设置x:笔的名称属性时,我得到了一个错误,找不到目标,所以我将模板更改为以下内容:
<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>
现在应用程序构建没有错误但是当我运行它时我得到错误:
无法将类型为“System.Windows.TextDecoration”的内容添加到System.Windows.TextDecorationCollection类型的对象
有什么问题?或者可能有另一种方式?
答案 0 :(得分:3)
无法重现这样的错误。请注意,即使没有错误,您的触发器也不会起作用,因为您直接在目标对象上设置了precedence超过触发器设置器的值。
以下是变量TextDecorations
的示例,它可以在鼠标悬停时进行更改:
<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>
答案 1 :(得分:0)
我的方法类似,但没有模板控制。
<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>