这是我正在使用的一个例子:
<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提供的解决方案是实现理想结果的最简单方法.
但是,还有其他可能的解决方案:
WrapPanel
为其设置Margin
和/或其Padding
所有子项实现您自己的附加属性/行为.有关详细信息,请参阅Josh Smith撰写的CodeProject文章.WrapPanel
并只添加所需的属性并覆盖适当的方法,以便为所有子元素设置Margin
/ Padding
.Style
定义从中移动Window.Resources
到WrapPanel.Resources
,x:Key
从中删除属性Style
,并Style="{StaticResource ButtonStyle}"
从中删除所有Button
s.这样,Style
它就适用于所有 Button
s的孩子WrapPanel
.如果您还有其他控件作为子项,则可以将TargetType
for 更改为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)
这是将边距设置为面板的几个子节点的最简单,最快捷的方法,即使它们的类型不同.(即按钮,文本框,组合框等)
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)
归档时间: |
|
查看次数: |
26716 次 |
最近记录: |