在单击事件中将 TextDecoration 对象添加到 TextBlock.TextDecorations 集合(例如):
XAML:
<Button Click="Button_Click_1">
<TextBlock>
Book 1
</TextBlock>
</Button>
和处理程序:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
// ... your logic
var button = (Button)sender;
var textBlock = (TextBlock)button.Content;
// if decoration wasn't already inserted
//
if (!textBlock.TextDecorations.Any())
textBlock.TextDecorations.Add(new TextDecoration { Location = TextDecorationLocation.Strikethrough });
}
更新:回答您的评论 - 最简单的方法
XAML
<Button x:Name="button1" Click="Button_Click_1">
<TextBlock>
Book 1
</TextBlock>
</Button>
<Button x:Name="button2" Click="Button_Click_2">
<TextBlock>
Book 2
</TextBlock>
</Button>
代码:
private void SetStrikethrough(Button b, Boolean strikethrough)
{
var textBlock = (TextBlock)b.Content;
if (strikethrough)
{
if (!textBlock.TextDecorations.Any())
textBlock.TextDecorations.Add(
new TextDecoration { Location = TextDecorationLocation.Strikethrough });
}
else
{
textBlock.TextDecorations.Clear();
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
SetStrikethrough(button1, true);
SetStrikethrough(button2, false);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
SetStrikethrough(button2, true);
SetStrikethrough(button1, false);
}
请注意,此代码始终假定按钮内容是文本块。只是为了简单。