CodeEngine
CodeEngine

Reputation: 296

How to be able to have a two line text on Button content WPF ? And be able to click on text and activate Button1_click

The code is shown below. When I try to click on the middle of the button you are actually clicking the TextBlock and not triggering the Button2_click event. Only if you click outside of the TextBlock and still inside of the Button it would trigger the event.

What is the right way of having a two line text inside the content of the Button.I'm a new on StackOverFlow so I don't have enough reputation to add a picture. Please some Help with this question. Thanks for the Help in Advance.

<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>

Upvotes: 0

Views: 1363

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

Put your TextBlock inside your button:

<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>

This will implicitly set Button.Content, it's an equivalent of:

<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>

Your original code overlays the TextBlock over the button, so both of these elements aren't related.

Upvotes: 2

Related Questions