伙计,这是在 WPF 中设置 UI 元素位置的最糟糕的方法!
将您的 XAML 重构为如下所示:
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListView Grid.Row="0" Name="TableList" Margin="5"/>
<Button Grid.Row="1" Name="button3" Content="OK"
Margin="5"
Width="75"
HorizontalAlignment="Right"/>
</Grid>
你看?有一个Grid处理其所有孩子的位置(在本例中为 aListView和 a Button)。
Button放在第二个,Row与右侧(HorizontalAlignment属性)对齐。
Grid和它的孩子都有Margin=5。这保证了每个孩子的边距对于 aiacent 孩子和Grid.
此外, theListView和 theButton完美对齐。
您的方法的问题是您设置了Button Width and itsLeft Margin 和its Right Margin。也许总数不正确,因为边框Window吃掉了一些像素,或者只是 WPF 无法同时处理所有信息而错过了计算,谁知道呢,但结果是您必须至少保留一个参数 free。在我的示例中,我离开Margins了窗口。这些Margin=5仅设置相Margin对于其他控件的相对尊重,但Button距离左边框的距离Window是我留给 WPF 图形引擎计算的。