C# 两个箱子的边距为';不正确

C# 两个箱子的边距为';不正确,c#,wpf,margins,C#,Wpf,Margins,所以我有两个盒子,这里的目标是让两个盒子在窗口边缘有完全相同的边距。不幸的是,在WPF中,您只能从左侧获得边距,而不能同时从左侧和左侧获得边距,因此为了确保这一点,我有以下公式: 第二个框的边距等于窗口宽度减去第一个框的边距减去框的大小 这可以用数学来证明,它确实有效。所以我确实让我的程序来解决这个问题,因为它是反应性的。现在我有了这个图像: WPF窗口 中文本是证明这个计算是正确的。(正如你所知,每个盒子的宽度是500px)所以,第一个数字是第一个盒子的边距。第二个是第二个的边距,最后一个是

所以我有两个盒子,这里的目标是让两个盒子在窗口边缘有完全相同的边距。不幸的是,在WPF中,您只能从左侧获得边距,而不能同时从左侧和左侧获得边距,因此为了确保这一点,我有以下公式:

第二个框的边距等于窗口宽度减去第一个框的边距减去框的大小

这可以用数学来证明,它确实有效。所以我确实让我的程序来解决这个问题,因为它是反应性的。现在我有了这个图像:

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>

您要找的是一个网格,请将
列定义设置为矩形宽度+所需的边距。用于中间
宽度
的*表示“填满可用空间”。
Margin
参数设置为(左、上、右、下)。这允许您指定右侧
边距

<Window x:Class="ForStackoverflow.MainWindow"
    ...
    xmlns:local="clr-namespace:ForStackoverflow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition />
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>

        <Rectangle Margin="15 0 0 0" 
                   StrokeThickness="5" 
                   Stroke="Black"
                   Height="80"
                   Grid.Column="0"/>

        <Rectangle Margin="0 0 15 0" 
                   StrokeThickness="5" 
                   Stroke="Black"
                   Height="80"
                   Grid.Column="2"/>
    </Grid>
</Window>


您最好使用
网格
将它们隔开,然后显示您的XAML。我在定义页边距时从来没有遇到过问题。@gdir我添加了它。@User1178我建议您阅读WPF中的布局面板。选择正确的布局面板使WPF非常容易。试图像WinForms那样设置边距不是正确的方法。如果1936是窗口的宽度,请注意它包括(两倍)窗口边框的宽度。请注意,您可以轻松地将右侧边距和水平对齐设置为右侧。