在 MouseEnter 上将文本块中的文本更改为斜体

Changing text in textblock to italicized on MouseEnter

我有一个包含一些非斜体文本的文本块。当鼠标进入文本块时,通过使用隐藏代码改变文本。我希望后面的代码也能够将文本更改为斜体。这是我目前所拥有的:

XAML:

<TextBlock x:Name="block1"
   Background="Cyan"
   Foreground="{StaticResource myBrush2}"
   Grid.Column="0"
   Grid.Row="0"
   Height="30"
   HorizontalAlignment="Center"
   MouseEnter="TextBlock_MouseEnter"
   MouseLeave="TextBlock_MouseLeave"
   Padding="0,7,0,0"
   Text ="Hover Me!"
   TextAlignment="Center"
   Width="100"/>

隐藏代码(C#):

public void TextBlock_MouseEnter(object sender, MouseEventArgs e)
{
    string blockName = ((TextBlock)sender).Name;
    var block = sender as TextBlock;
    if (block != null && blockName == "block1")
    {
        block.Text = "Yo! I'm TextBlock1";
    }
}

我研究过 System.Drawing 和 FontStyle.Italic 的使用;虽然我没能成功。

这就是 XAML

设计的
<TextBlock x:Name="block1"
   Background="Cyan"
   Foreground="{StaticResource myBrush2}"
   Grid.Column="0"
   Grid.Row="0"
   Height="30"
   HorizontalAlignment="Center"
   MouseEnter="TextBlock_MouseEnter"
   MouseLeave="TextBlock_MouseLeave"
   Padding="0,7,0,0"
   Text ="Hover Me!"
   TextAlignment="Center"
   Width="100">
            <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="FontStyle" Value="Italic" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
        </TextBlock>

但是,如果您真的想要,这里有一个示例,说明您可以如何从代码隐藏中实现该功能。

private void block1_MouseEnter(object sender, MouseEventArgs e)
{
     SetFontStyle(FontStyles.Italic);
}

private void block1_MouseLeave(object sender, MouseEventArgs e)
{
     SetFontStyle(FontStyles.Normal);
}
private void SetFontStyle(FontStyle style)
{
     block1.FontStyle = style;
}