Applying WPF styles to Child Items

独自空忆成欢 提交于 2019-12-08 16:11:54

问题


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>

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.

Thanks Ian


回答1:


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>




回答2:


This seems to answer a similar question to yours: Apply style to all TreeViewItem

If that doesn't work then I'm not too sure about how it would be done in XAML but you could add the style in the code-behind with:

Control element;

for (int i = 0; i < Grid1.Children.Count; i++)
{
    element = (Control) Grid1.Children[i];
    element.Style = (Style) FindResource("DefaultMargins");
}

Edit: Grid1 refers to a x:Name="Grid1" property added to the XAML grid (poor naming I know).




回答3:


Place elements inside ItemsControl with ItemsPanel set to Grid and ItemContainerStyle to your style:

<Window.Resources>
  <Style x:Key="DefaultMargins">
    <Setter Property="Control.Margin"
            Value="3, 3, 3, 3" />
    <Setter Property="Control.FontSize"
            Value="50" />
  </Style>
</Window.Resources>
<ItemsControl ItemContainerStyle="{StaticResource DefaultMargins}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <Grid>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="3*" />
          <ColumnDefinition Width="3*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="3*" />
          <RowDefinition Height="3*" />
        </Grid.RowDefinitions>
      </Grid>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>

  <Button Grid.Row="0"
          Grid.Column="0"
          Name="button1">Button</Button>

</ItemsControl>

This has a drawback of not working well with designer.



来源:https://stackoverflow.com/questions/1465806/applying-wpf-styles-to-child-items

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!