简体   繁体   English

为什么两个按钮仍然重叠

[英]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.

相关问题 使用 onclick 按钮在 Powershell 上的 Xaml 网格上重叠按钮 - Overlap Buttons on a Xaml Grid on Powershell with onclick button 为什么这个工具提示仍然启用? - Why is this tooltip still enabled? 如果没有太多按钮并且大多数视图在不同视图中重叠,那么组织功能区中的按钮的正确方法是什么? - What is the correct way to organize the Buttons of the ribbon when there are not too many buttons and most overlap for different Views? 如何动态地将按钮放置在画布中,以使其不会相互重叠? - How to place buttons in a canvas dynamically so that it won't overlap each other? 我可以在工具栏按钮上使用DataTemplate并仍然使名称有意义吗? - Can I use a DataTemplate for toolbar buttons and still make the name meaningful? 为什么DatePicker的Text属性仍然包含值? - Why Text property of a DatePicker still contains value? 为什么我的按钮在XAML设计器中不可见? - Why are my buttons not visible in the XAML designer? 为什么按钮在堆栈面板中没有水平对齐? - why aren't the buttons aligning horizontally in the stackpanel? 为什么我的某些按钮在运行时被禁用? - Why are some of my buttons disabled at runtime? 为什么单选按钮没有出现在Windows 8的弹出窗口中? - Why do radio buttons not appear in Windows 8 on a popup?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM