如何为WrapPanel的内部控件设置边距

And*_*aew 27 wpf wpf-controls

这是我正在使用的一个例子:

<Window x:Class="WpfApplication2.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">
<StackPanel>
    <WrapPanel Orientation="Horizontal" TextElement.FontSize="30" TextElement.FontStyle="Italic"  >
        <Button Content="test1" Margin="10,0" Padding="10,10" />
        <Button Content="test2" Margin="10,0" Padding="10,10" />
        <Button Content="test3" Margin="10,0" Padding="10,10" />
        <Button Content="test4" Margin="10,0" Padding="10,10" />
        <Button Content="test5" Margin="10,0" Padding="10,10" />
    </WrapPanel>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

如您所见,我的包装面板有几个按钮.每个按钮具有相同的边距和填充.

问题是,有没有办法为包装面板设置边距和填充,因此包装面板内的每个元素都可以使用它的值?

为了设置内部元素的字体,我可以使用"TextElement"附加属性提供程序.有没有类似的方法我可以设置内部控件的边距和填充?

这使得代码更短,让我只指定一次Margin和Padding,而不是为面板中的每个控件设置它.

谢谢!

geh*_*hho 23

James Hay提供的解决方案是实现理想结果的最简单方法.

但是,还有其他可能的解决方案:

  1. 您可以WrapPanel为其设置Margin和/或其Padding所有子项实现您自己的附加属性/行为.有关详细信息,请参阅Josh Smith撰写的CodeProject文章.
  2. 您可以创建自己的面板,该面板继承WrapPanel并只添加所需的属性并覆盖适当的方法,以便为所有子元素设置Margin/ Padding.
  3. 您也可以将Style定义从中移动Window.ResourcesWrapPanel.Resources,x:Key从中删除属性Style,并Style="{StaticResource ButtonStyle}"从中删除所有Buttons.这样,Style它就适用于所有 Button s的孩子WrapPanel.如果您还有其他控件作为子项,则可以将TargetTypefor 更改为Style适当的公共基类型(例如FrameworkElement):

<StackPanel>
  <WrapPanel Orientation="Horizontal">
    <WrapPanel.Resources>
      <Style TargetType="{x:Type Button}">
        <Setter Property="Margin" Value="10,0" />
        <Setter Property="Padding" Value="10,10" />
      </Style>
    </WrapPanel.Resources>

    <Button Content="test1" />
    <Button Content="test2" />
    <Button Content="test3" />
    <Button Content="test4" />
    <Button Content="test5" />
  </WrapPanel>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

但请注意,这将影响其中的所有 Button实例WrapPanel,而不仅仅是其直接的孩子!


Ela*_*atz 17

另一种不错的方法可以在这里看到:http: //blogs.microsoft.co.il/blogs/eladkatz/archive/2011/05/29/what-is-the-easiest-way-to-set-spacing-between-项功能于stackpanel.aspx

它显示了如何创建附加行为,以便像这样的语法可以工作:

<StackPanel local:MarginSetter.Margin="5">
   <TextBox Text="hello" />
   <Button Content="hello" />
   <Button Content="hello" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

这是将边距设置为面板的几个子节点的最简单,最快捷的方法,即使它们的类型不同.(即按钮,文本框,组合框等)

  • 真的很好,我打算用它.我不是WPF专家,但我觉得必须有一种方法来设置所有内容的余地,而不使用在运行时执行的代码. (2认同)
  • 您博客的链接似乎已损坏。您可以更新它或者更好地复制这里的内容吗? (2认同)

Jam*_*Hay 7

WrapPanel没有任何为其所有子项添加填充或边距的属性.您可能想要的是每个按钮共享的样式.就像是:

<Window x:Class="WpfApplication2.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>
        <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Margin" Value="10,0" />
            <Setter Property="Padding" Value="10,10" />
        </Style>
    </Window.Resources>

    <StackPanel>
      <WrapPanel Orientation="Horizontal"  >
         <Button Content="test1" Style="{StaticResource ButtonStyle}" />
         <Button Content="test2" Style="{StaticResource ButtonStyle}" />
         <Button Content="test3" Style="{StaticResource ButtonStyle}" />
         <Button Content="test4" Style="{StaticResource ButtonStyle}" />
         <Button Content="test5" Style="{StaticResource ButtonStyle}" />
       </WrapPanel>
    </StackPanel>
   </Window>
Run Code Online (Sandbox Code Playgroud)