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

时间:2021-11-13 22:18:31

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?

在以下XAML代码中,按钮文本缺少一半。我可以更改Margin属性,很明显,在250px后隐藏了内容。为什么会这样,我该如何解决?

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

3 个解决方案

#1


15  

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上还有Horizo​​ntalAlignment = 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.

如果切换到Horizo​​ntalAlignment =“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". Using Margin="200,0" means that both the left and the right Margins will be 200.

如果只想要左边距为200,则使用Margin =“200,0,0,0”。使用Margin =“200,0”意味着左边距和右边距都是200。

#2


1  

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>

#3


1  

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.

这消除了右侧的边距并允许整个按钮显示。

#1


15  

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上还有Horizo​​ntalAlignment = 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.

如果切换到Horizo​​ntalAlignment =“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". Using Margin="200,0" means that both the left and the right Margins will be 200.

如果只想要左边距为200,则使用Margin =“200,0,0,0”。使用Margin =“200,0”意味着左边距和右边距都是200。

#2


1  

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>

#3


1  

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.

这消除了右侧的边距并允许整个按钮显示。