【发布时间】:2018-06-13 17:30:03
【问题描述】:
所以我有两个框,这里的目标是让两个框从窗口边缘具有完全相同的边距。不幸的是,在 WPF 中,您只能从左侧获得边距,而不是两者都获得,所以为了确保这一点,我有以下公式;
第二个框的边距等于窗口宽度减去第一个框的边距再减去框的大小。
这可以用数学来支持,它确实有效。所以我确实让我的程序来解决这个问题,因为它是反应性的。所以现在我有了这张图片:
中间的文字是为了证明这个计算是正确的。 (只是让你知道,每个框都是 500px 宽)所以,第一个数字是第一个框的边距。第二个是第二个的边距,最后一个是窗口大小。算一下,你有这个等式:
(1936 - 88) - 500 = 1348
这确实有效,但是,如果您查看图像,右侧的框比左侧的框更靠近边缘一点。如果您调整它的大小,也会发生这种情况,而不仅仅是在最大化时。怎么了,我该如何解决这个问题?
这是我的 XAML 代码:
<Window x:Class="Horizon_Chat.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Horizon_Chat"
xmlns:i="clr-namespace:Horizon_Chat"
i:WindowEx.ShowIcon = "false"
mc:Ignorable="d"
Title="Horizon Chat" Height="900" Width="1280" WindowStartupLocation="CenterScreen" MinWidth="600" MinHeight="600" WindowState="Maximized">
<Grid>
<TextBlock x:Name="welcomeTitleText" HorizontalAlignment="Center" Margin="0,-200,0,0" TextWrapping="Wrap" Text="Hello Camden!" VerticalAlignment="Center" FontFamily="Muli" FontSize="60"/>
<Border x:Name="topBorder" BorderBrush="#FF646464" BorderThickness="0,0,0,4" HorizontalAlignment="Left" Height="41" VerticalAlignment="Top" Width="1273" Margin="0,32,0,0"/>
<Ellipse Fill="#FF30B44E" HorizontalAlignment="Left" Height="50" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="50" StrokeThickness="3"/>
<Border x:Name="chatsBox" BorderBrush="Black" BorderThickness="3" HorizontalAlignment="Left" Height="250" Margin="58,594,0,0" VerticalAlignment="Top" Width="500"/>
<Border x:Name="friendsBox" BorderBrush="Black" BorderThickness="3" HorizontalAlignment="Left" Height="250" Margin="724,594,0,0" VerticalAlignment="Top" Width="500"/>
</Grid>
</Window>
【问题讨论】:
-
您最好使用
Grid将它们隔开 -
请出示您的 XAML。我从来没有遇到过定义边距的问题。
-
@gdir 我加了。
-
@User1178 我建议您阅读 WPF 中的布局面板。选择正确的布局面板使 WPF 变得非常容易。尝试像在 WinForms 中那样设置边距不是正确的方法。 WPF layout panels
-
如果 1936 是窗口的宽度,请注意它包括(两倍)窗口边框的宽度。请注意,您可以轻松地将右边距和 HorizontalAlignment 设置为 Right。