【问题标题】:Applying WPF styles to Child Items将 WPF 样式应用于子项
【发布时间】:2010-11-30 18:00:18
【问题描述】:

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

我希望以下内容会起作用:

<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>

但 Margin 被忽略,它不支持属性值继承。是否有一种简单的替代方法可以将边距应用于网格的每个“孩子”?我知道可以在 CSS 中实现这种东西,我们的一些开发人员对使用这种结构很感兴趣。

谢谢 伊恩

【问题讨论】:

  • 您找到答案了吗?我问是因为我有同样的问题。

标签: wpf grid styles


【解决方案1】:

ItemsControl 中放置元素,并将ItemsPanel 设置为GridItemContainerStyle 以符合您的风格:

<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>

这有一个缺点是不能很好地与设计师合作。

【讨论】:

  • 这似乎正是我想要的。如果我稍后再回来,试试看它有效,然后我会投票。
【解决方案2】:

可以按类型指定样式,并将其限制在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>

【讨论】:

  • 这也将适用于 children 的孩子等等。使用ItemsControl.ItemContainerStyle查看我的答案以获得确切的解决方案。
  • 我尝试了您的代码,但无法使其正常工作。深入研究后,我发现当样式的 TargetType 属性设置为控件的基类时,WPF 不会将样式应用于控件。 stackoverflow.com/questions/1026635/…
  • 这曾经有效。我不发布我不吃狗粮的代码。
【解决方案3】:

这似乎回答了与您类似的问题: Apply style to all TreeViewItem

如果这不起作用,那么我不太确定如何在 XAML 中完成,但您可以在代码隐藏中添加样式:

Control element;

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

编辑:Grid1 指的是添加到 XAML 网格的 x:Name="Grid1" 属性(我知道命名很糟糕)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多