[英]Why the 2 buttons are still overlap
Here is my xaml: 这是我的xaml:
<Grid>
<Button x:Name="top1" Content="top" Width="200" Height="50" VerticalAlignment="Top" HorizontalAlignment="Center"/>
<Button x:Name="top2" Content="top" Width="100" Height="25" VerticalAlignment="Top" HorizontalAlignment="Center"/>
<Button x:Name="left" Content="left" Width="200" Height="50" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="0,0,0,0" Click="Button_Click"/>
</Grid>
When I click left button, I want the top2 button go left to avoid overlapped with top1 button. 当我单击左按钮时,我希望top2按钮向左移动以避免与top1按钮重叠。 Here is my background code:
这是我的背景代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
var offset = this.top1.ActualWidth + this.top2.ActualWidth / 2;
this.top2.Margin = new Thickness(0, 0, offset, 0);
}
But in the result, top1 and top2 are still overlapped. 但是结果是top1和top2仍然重叠。
Why this happens? 为什么会这样? and how to resolve?
以及如何解决?
使用行定义,列定义和设置按钮的可见性可以解决该问题
The buttons are horizontally centered within given space. 按钮在给定空间内水平居中。 To solve this, I have to double the margin:
为了解决这个问题,我必须加倍保证金:
private void Button_Click(object sender, RoutedEventArgs e)
{
var offset = this.top1.ActualWidth + this.top2.ActualWidth;
this.top2.Margin = new Thickness(0, 0, offset, 0);
}
But this is correct only when the top2 doesn't reach over the edge of its layout. 但这仅在top2没有超过其布局边缘时才是正确的。 If top2 is partially out of left edge, we don't need to double the margin then.
如果top2部分位于左边缘之外,则无需再将页边距加倍。 And both left side and right side of top2 are trimmed.
并且修剪了top2的左侧和右侧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.