简体   繁体   English

为什么这个按钮会被切断?

[英]Why is this button cut off?

In the following XAML code the button text is half missing. 在以下XAML代码中,按钮文本缺少一半。 I can change the Margin property and it becomes obvious that after 250px the content is hidden. 我可以更改Margin属性,很明显,在250px后隐藏了内容。 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>

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. 水平边距为200,按钮宽度为125,这意味着正确显示控件所需的总宽度约为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. 你的DockPanel上还有HorizontalAlignment=Left" ,这意味着它将以所需的任何宽度绘制内容,并将其对齐到屏幕的左侧,而不是拉伸它以填充所有可用空间。这意味着它阻止了控件两侧的空间为200,并在剩余空间中绘制按钮。如果此剩余空间小于125,则将裁剪图像。

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. 如果你切换到HorizontalAlignment="Stretch" ,那么它将首先绘制控件(带边距),然后拉伸它的大小,使其适合所有可用空间,这样整个控件都会调整大小而不是裁剪。

You might be interested in reading this MSDN article on Alignment, Margins, and Padding in WPF. 您可能有兴趣阅读有关 WPF中的对齐,边距和填充的MSDN文章

Edit 编辑

If you want only the Left margin to be 200, then use Margin="200,0,0,0" . 如果想要Left距为200,则使用Margin="200,0,0,0" Using Margin="200,0" means that both the left and the right Margins will be 200. 使用Margin="200,0"意味着左边距和右边距都是200。

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>

The problem is that the button Margin is set as: 问题是按钮Margin设置为:

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. 这消除了右侧的边距并允许整个按钮显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM