如何在Button内容WPF上有两行文字?并且能够单击文本并激活Button1_click

时间:2014-10-01 15:28:44

标签: c# wpf

代码如下所示。当我尝试单击按钮的中间时,您实际上是单击TextBlock而不是触发Button2_click事件。只有当您单击TextBlock外部并仍然在Button内部时,它才会触发事件。

在Button的内容中有两行文本的正确方法是什么。我是StackOverFlow的新手,所以我没有足够的声誉来添加图片。请帮助解决这个问题。感谢您提前帮助。

<Button Content="" Height="77" HorizontalAlignment="Left" Margin="70,136,0,0" Name="Button2" VerticalAlignment="Top" Width="202" />
<TextBlock TextAlignment="Center" Padding="2" Margin="80,152,0,135" FontSize="16" HorizontalAlignment="Left" Width="181" TextWrapping="NoWrap" Foreground="#FF8787DB" FontWeight="Bold"> Calculate CTR Losses<LineBreak></LineBreak> (From FTD Data)</TextBlock>

1 个答案:

答案 0 :(得分:2)

TextBlock放在按钮内:

<Button HorizontalAlignment="Left" Margin="70,136,0,0" Name="Button2" VerticalAlignment="Top">
    <TextBlock Padding="2" FontSize="16" TextWrapping="NoWrap" Foreground="#FF8787DB" FontWeight="Bold">
        Calculate CTR Losses
        <LineBreak />
        (From FTD Data)
     </TextBlock>
</Button>

这将隐式设置Button.Content,它相当于:

<Button HorizontalAlignment="Left" Margin="70,136,0,0" Name="Button2" VerticalAlignment="Top">
    <Button.Content>
        <TextBlock Padding="2" FontSize="16" TextWrapping="NoWrap" Foreground="#FF8787DB" FontWeight="Bold">
            Calculate CTR Losses
            <LineBreak />
            (From FTD Data)
        </TextBlock>
    </Button.Content>
</Button>

您的原始代码会将TextBlock覆盖在按钮上,因此这些元素都不相关。