将WPF样式应用于子项目 [英] Applying WPF styles to Child Items

查看:97
本文介绍了将WPF样式应用于子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我有一个网格,在我的网格中有一些控件。我不希望为这些控件中的每一个设置边距,而是希望创建一种样式来设置我放入网格的任何控件的边距。这是可能的吗?

Lets say I have a grid, within my grid I have a number of controls. Instead of setting the margin for each of these controls, I wish to create a style to set the margin for ANY control I drop into a grid. Is this possible?

我希望以下方法可行:

I was hoping that the following would work:

<Window.Resources>
    <Style x:Key="DefaultMargins">
        <Setter Property="Control.Margin" Value="3, 3, 3, 3"/>
        <Setter Property="Control.FontSize" Value="50"/>
    </Style>
</Window.Resources>
<Grid Style="{StaticResource DefaultMargins}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="3*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="3*"/>
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Grid.Column="0" Name="button1">Button</Button>
</Grid>

但是保证金被忽略,它不支持属性值继承。是否有一个简单的选择,将边距应用到网格的每个孩子?我明白可以在CSS中实现这种功能,并且我们的一些开发人员有兴趣使用这种构造。

But the Margin is ignored, it not supporting property value inheritance. Is there a simple alternative to apply the margins to each 'child' of the grid? I understand that it is possible to achieve this sort of thing in CSS and some of our developers are interested in using this sort of construct.

感谢
Ian

Thanks Ian

推荐答案

您可以按类型指定样式,并将其限制为 Grid $ b

You can specify the style by type and constrain it to the scope of the Grid:

    <Grid>
<Grid.Resources>
    <Style TargetType="{x:Type Control}">
        <Setter Property="Control.Margin" Value="3, 3, 3, 3"/>
        <Setter Property="Control.FontSize" Value="50"/>
    </Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="3*"/>
    <ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="3*"/>
    <RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="0" Name="button1">Button</Button>

这篇关于将WPF样式应用于子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆