Textblock样式切换!

时间:2010-10-15 08:26:05

标签: c# .net wpf xaml textblock

我有一个样式,当它被鼠标移动时会强调文本块...当我点击它以将其字体粗细改为粗体(选中)时,我需要什么... 任何想法?

1 个答案:

答案 0 :(得分:3)

dnr3所说的代码示例,一个模板化的ToggleButton

<Style x:Key="BoldWhenClickedTextBlock" TargetType="ToggleButton" BasedOn="{StaticResource {x:Type ToggleButton}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <TextBlock x:Name="c_toggleButtonTextBlock" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type ToggleButton}}, Path=Content}"/>
                <ControlTemplate.Triggers>
                    <Trigger Property="ToggleButton.IsMouseOver" Value="True">
                        <Setter TargetName="c_toggleButtonTextBlock" Property="TextDecorations" Value="Underline"/>
                    </Trigger>
                    <Trigger Property="ToggleButton.IsChecked" Value="True">
                        <Setter TargetName="c_toggleButtonTextBlock" Property="FontWeight" Value="Bold" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后“TextBlock”ToggleButton可以将其与

一起使用
<ToggleButton Style="{StaticResource BoldWhenClickedTextBlock}" Content="My Text.."/>