1

我有一个带有 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 和按钮

谢谢

4

1 回答 1

2

在单击事件中将 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);
        }

请注意,此代码始终假定按钮内容是文本块。只是为了简单。

于 2013-01-31T10:03:32.147 回答