dotancohen
dotancohen

Reputation: 31471

Why is this button cut off?

In the following XAML code the button text is half missing. I can change the Margin property and it becomes obvious that after 250px the content is hidden. Why is that, and how can I fix it?

<Window x:Class="InnerInterface.InventoryManagement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="someWindow" Height="500" Width="500">
    <DockPanel HorizontalAlignment="Left" Name="dockPanel1" VerticalAlignment="Top">
        <Grid DockPanel.Dock="Top">
            <Button Name="buttonReturnToMainMenu" Content="someButton" Margin="200,0" Width="125" />
        </Grid>
    </DockPanel>
</Window>

Upvotes: 4

Views: 16984

Answers (3)

Rachel
Rachel

Reputation: 132548

You have a horizontal margin of 200, and a button width of 125, which means the total width needed to show the control properly is about 525.

You also have HorizontalAlignment=Left" on your DockPanel, which means it will draw the content at whatever width it needs and align it to the left side of the screen instead of stretching it to fill all available space. This means it is blocking out a space of 200 on either side of the control, and drawing the button in the remaining space. If this remaining space is less than 125, the image will be cropped.

If you switch to HorizontalAlignment="Stretch", then it will draw the control first (with margins), then stretch it's size so it fits all available space, so the entire control gets resized rather than cropped.

You might be interested in reading this MSDN article on Alignment, Margins, and Padding in WPF.

Edit

If you want only the Left margin to be 200, then use Margin="200,0,0,0". Using Margin="200,0" means that both the left and the right Margins will be 200.

Upvotes: 16

dotancohen
dotancohen

Reputation: 31471

The problem is that the button Margin is set as:

Margin="200,0"

It should be set as:

Margin="200,0,0,0"

This eliminates the margin on the right side and allows the whole button to show.

Upvotes: 1

NestorArturo
NestorArturo

Reputation: 2516

Not really sure about your exact problem, but maybe this should help:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="500">
    <DockPanel HorizontalAlignment="Stretch" Name="dockPanel1" VerticalAlignment="Top">
        <Grid DockPanel.Dock="Top" >
            <Button Name="buttonReturnToMainMenu" Content="someButton" Width="125"  />
        </Grid>
    </DockPanel>
</Window>

Upvotes: 1

Related Questions