WPF BUTTON WITH MULTIPLE TEXT ELEMENTS

Ashish Pandey

Total Post:128

Points:898
Posted by  Ashish Pandey
WPF 
WPF
 1208  View(s)
Ratings:
Rate this:

I am trying to create buttons for a 10-foot GUI using WPF. Each button requires a little more data than just a single text string and an image, maybe 2-3 strings located in different positions and some imagery.

I have tried

 <Button Height="52" HorizontalAlignment="Stretch" Name="button1"  Width="407">

        <Button.Content>

        <DockPanel LastChildFill="True" HorizontalAlignment="Stretch">

            <TextBlock Name="textBloczk2" Text="ABC" TextAlignment="Left"   DockPanel.Dock="Left"/>

            <TextBlock Name="textBlxock1" Text="CDE" TextAlignment="Right" DockPanel.Dock="Right"/>

        </DockPanel>

        </Button.Content>

    </Button>

But no matter which inner container I use, the button seems to disregard the layout from the DockPanel and the combined text ends up in the middle of the button. Am I doing something wrong or should I be using a different outer container ?

  1. Sumit Kesarwani

    Post:378

    Points:2694
    Re: WPF Button with multiple text elements

    Hi Ashish,


    The problem seems to be that the DockPanel's width is so small that the Right and Left panels are the same width as your TextBlocks.

    This seems to work as expected (setting the width of the DockPanel):

        <Button Height="52" HorizontalAlignment="Stretch" Name="button1"  Width="407">

            <Button.Content>

                <DockPanel LastChildFill="True" Width="300">

                    <TextBlock Name="textBloczk1" Text="Left" DockPanel.Dock="Left" />

                    <TextBlock Name="textBlxock2" Text="Right" DockPanel.Dock="Right" />

                    <TextBlock Name="textBlxock3" Text="Top" DockPanel.Dock="Top" />

                    <TextBlock Name="textBlxock4" Text="Bottom" DockPanel.Dock="Bottom" />

                </DockPanel>

            </Button.Content>

        </Button>

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!