【问题标题】:How to set a default Margin for all the controls on all my WPF windows?如何为所有 WPF 窗口上的所有控件设置默认边距?
【发布时间】:2012-02-18 05:19:26
【问题描述】:

我想在所有窗口上放置的所有控件上设置默认边距 3,并且能够仅在少数项目上覆盖此值。

我见过一些方法,比如做样式,但是我需要为所有东西设置样式,我更喜欢一些东西,而不是所有控件一起做的事情。我见过像 MarginSetter 这样的其他东西,但看起来它没有遍历子面板。我只希望我在窗口中放置的控件上的边距,与可视树的边框或其他内容无关。

对我来说看起来很基本。有什么想法吗?

提前致谢。

【问题讨论】:

  • 边框是控件。使用样式有什么问题?如果您使用 Wpf,请查看隐式样式 - 可以应用于特定类型的那些(SL5 应该也有它们)。
  • @Dmitry:问题是我需要为我使用的任何人创建的每个控件创建一个样式。考虑到所有控件都有基类并且使用继承来避免这种事情(编程时重复的东西总是错误的),这看起来不是一个很好的解决方案
  • 你读过我说的吗 - 使用隐式样式 - 它们被应用于类型而不是实例级别。
  • @Dmitry:我说的是为直到今天或将来创建的每种类型的控件创建一种样式。
  • 我明白了,样式是实现您所寻找的最简单和最正确的方法。

标签: wpf window controls margin


【解决方案1】:

您可以在按钮样式中应用边距。当您在 StackPanel 中使用这种样式的按钮时,wpf 将应用需要的间距。 例如 在资源字典或其他内容中定义:

 <Style x:Key="myButtonStyle"  TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10"/>
....
</Style>

然后在你的 StackPanel xaml 定义中:

<StackPanel>
   <Border BorderThickness="0"/>
   <Button x:Name="VertBut1" Style="{StaticResource myButtonStyle}"      Content="Button1"/>
   <Button x:Name="VertBut2" Style="{StaticResource myButtonStyle}"      Content="Button2"/>
   <Button x:Name="VertBut3" Style="{StaticResource myButtonStyle}"      Content="Button3"/>
</StackPanel>

问候 乔治

【讨论】:

  • 是否可以将样式添加到堆栈面板并将该样式向下传播到所有子元素?
【解决方案2】:

您可以通过引用资源中定义的“厚度”来链接所有保证金属性。我只是在一个项目中这样做...

<!-- somwhere in a resource-->
<Thickness  x:Key="CommonMargin" Left="0" Right="14" Top="6" Bottom="0" />

<!-- Inside of a Style -->
<Style TargetType="{x:Type Control}" x:Key="MyStyle">
     <Setter Property="Margin" Value="{StaticResource CommonMargin}" />
</Style>
<!-- Then call the style in a control -->
<Button Style="{StaticResource MyStyle}" />

<!-- Or directly on a Control -->
<Button Margin="{StaticResource CommonMargin}" />

对我来说,关键是弄清楚边距是由“厚度”定义的。让我知道这是否足够清楚,或者您是否需要我将其放入一个完整的 XAML 示例中。

【讨论】:

    【解决方案3】:

    我能找到的唯一解决方案是将样式应用于您在窗口上使用的每个控件(我知道这不是您想要的)。如果您只使用几种不同的控件类型,那么执行以下操作并不太麻烦:

    <Window x:Class="WpfApplication7.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <!-- One style for each *type* of control on the window -->
            <Style TargetType="TextBox">
                <Setter Property="Margin" Value="10"/>
            </Style>
            <Style TargetType="TextBlock">
                <Setter Property="Margin" Value="10"/>
            </Style>
        </Window.Resources>
        <StackPanel>
            <TextBox Text="TextBox"/>
            <TextBlock Text="TextBlock"/>
        </StackPanel>
    </Window>
    

    祝你好运……

    【讨论】:

    • 好吧,事情是 Windows 窗体有一个默认的边距值......而且一切都需要一个边距设置。我一直在寻找一个通用的解决方案来将它应用到我的所有应用程序中。看起来我必须为每个控件创建一个样式......这可能会伤害到我需要永远维护这该死的东西。
    • 是的,这绝对是 WPF 的一个缺点。如果我找到更好的解决方案,我会在这里发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2021-08-25
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-10-11
    相关资源
    最近更新 更多