【问题标题】:WPF: possible to apply Style to all items in a StackPanel regardless of their typeWPF:可以将样式应用于 StackPanel 中的所有项目,无论它们的类型如何
【发布时间】:2013-06-01 12:24:27
【问题描述】:

如果 StackPanel 包含多种类型的控件(例如 ComboBox、CheckBox、Grid),则在应用边距或填充样式时,是否可以在 XAML 中表达“您包含的所有项目”或“每个项目”的概念你的包含”并将样式应用于 StackPanel 本身,或者每个控件是否必须由其类型明确引用,如下所示?有没有办法说 TargetType=ANY?

<StackPanel>
   <StackPanel.Resources>
     <Style TargetType="{x:Type foo:ComboBox}">
         <Setter Property="Margin" Value="10,10,10,10"/>
     </Style>
     <Style TargetType="{x:Type foo:CheckBox}">
         <Setter Property="Margin" Value="10,10,10,10"/>
     </Style>
      <Style TargetType="{x:Type foo:GridView}">
         <Setter Property="Margin" Value="10,10,10,10"/>
     </Style>
</StackPanel.Resources>

【问题讨论】:

  • 是的,将样式定位到基本类型,例如控件

标签: wpf styles wpf-controls


【解决方案1】:
<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type Control}">
            <Style.Setters>
                <Setter Property="Margin" Value="5"/>
            </Style.Setters>
        </Style>
    </StackPanel.Resources>
    <ComboBox />
    <Label />
    <ListView />

【讨论】:

  • {x:Type Control} 不包括第三方自定义控件吗?上面的方法似乎不适用于他们。
  • 应该,我经常使用telerik,而且这种方法有效,因为即使RadioButton是从RadioButton派生的,RadioButton是从Button派生的,然后是Control......但是,这可能没有帮助你。所以我建议,首先在你的对象浏览器中查看第 3 方库,并弄清楚它们的基本类型是什么
  • 有趣。我正在使用最新的 Telerik WPF 控件。似乎无法让它工作。我在 RadDocking 容器的左窗格中的 StackPanel 中有一个 RadProgressBar 和一个 GridViewDataControl。 RadProgressBar 在 Telerik.Windows.Controls 中; BaseTypes: RangeBase -> Control -> FrameworkElement.
  • 你的其他控件有样式吗?如果是这样,那么为了让他们选择此示例中的样式,您将不得不以这种样式为基础。你必须给这个样式一个 x:Key="whateverName" 然后在你的样式中指定 BasedOn="{StaticResource whateverName}"
  • 尝试使用您在 XAML 中引用的命名空间,以便使用您的 Telerik 控件。假设您使用 x:telerik ....in 声明: TargetType="telerik:Control"
猜你喜欢
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
  • 2022-10-01
相关资源
最近更新 更多