WPF中元素间的单空间裕度

WPF中元素间的单空间裕度,wpf,mono,styles,margin,space,Wpf,Mono,Styles,Margin,Space,例如,我希望StackPanel中的子元素之间的空间相同。当对子元素使用相同的边距时,相邻元素之间的间距将加倍。我用了一个小技巧来解决这个问题,但在我看来,还有更优雅的解决方案。你有吗 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xam

例如,我希望StackPanel中的子元素之间的空间相同。当对子元素使用相同的边距时,相邻元素之间的间距将加倍。我用了一个小技巧来解决这个问题,但在我看来,还有更优雅的解决方案。你有吗

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MyButtonStyle" TargetType="Button">
    <Setter Property="Margin" Value="4,4,0,4" />
    ...
</Style>
<Style x:Key="LastMyButtonStyle" TargetType="Button" BasedOn="{StaticResource MyButton}">
    <Setter Property="Margin" Value="4" />
</Style>

...


我对所有按钮使用MyButtonStyle,但最后一个按钮除外,它使用LastMyButtonStyle。

将StackPanel放入另一个容器中,即边框,并将其边距设置为与按钮边距相同的值:

<Border>
    <StackPanel Orientation="Horizontal" Margin="2">
        <Button Margin="2" Content="Button 1"/>
        <Button Margin="2" Content="Button 2"/>
        <Button Margin="2" Content="Button 3"/>
    </StackPanel>
</Border>


当然,这是一个很好的解决方案。但它同样使用两种样式。我在寻找更多的东西。我很感兴趣我们如何在一种风格中实现同样的效果。也许我想要的太多了。)这只是一项研究。谢谢你的帮助和时间!当然可以,但Button和StackPanel只有一种样式,这意味着您可以将它们设置为默认样式。因此,您不需要将它们显式地应用于每个元素。现在我将选择这种方法。谢谢!