我有一个带3个按钮的WrapPanel。
<WrapPanel Orientation="Horizontal">
<Button Content="Book1" />
<Button Content="Book2" />
<Button Content="Book3" />
</WrapPanel>
如果我点击Book1,我会看到Book1的内容。如果我点击Book2,我会看到Book2等的内容。如果点击它,是否有任何命令可以触及按钮?在Html中,文本有“del”:
<del>Strikethrough</del>
我想要同样但是在wpf和Buttons
谢谢
答案 0 :(得分:2)
在Click事件中将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);
}
请注意,此代码始终假定按钮内容是文本块。为简单起见。