在HTML / CSS中,您可以定义一种可应用于多种类型元素的样式,例如:
.highlight {
color:red;
}
可以应用于P和DIV,例如:
<p class="highlight">this will be highlighted</p>
<div class="highlight">this will also be highlighted</div>
但是在XAML中你似乎必须为样式定义TargetType,否则会出错:
<Style x:Key="formRowLabel" TargetType="TextBlock">
有没有办法允许将XAML样式应用于多个元素,甚至可以像在CSS中那样将其保持打开状态?
答案 0 :(得分:67)
在编译期间检查WPF样式中的setter; CSS样式是动态应用的。
您必须指定一个类型,以便WPF可以将setter中的属性解析为该类型的依赖项属性。
您可以将目标类型设置为包含所需属性的基类,然后将该样式应用于派生类。例如,您可以为Control对象创建样式,然后将其应用于多种类型的控件(Button,TextBox,CheckBox等)
<Style x:Key="Highlight" TargetType="{x:Type Control}">
<Setter Property="Foreground" Value="Red"/>
</Style>
...
<Button Style="{StaticResource Highlight}" Content="Test"/>
<TextBox Style="{StaticResource Highlight}" Text="Test"/>
<CheckBox Style="{StaticResource Highlight}" Content="Test"/>
答案 1 :(得分:35)
<!-- Header text style -->
<Style x:Key="headerTextStyle">
<Setter Property="Label.VerticalAlignment" Value="Center"></Setter>
<Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
<Setter Property="Label.FontWeight" Value="Bold"></Setter>
<Setter Property="Label.FontSize" Value="18"></Setter>
<Setter Property="Label.Foreground" Value="#0066cc"></Setter>
</Style>
<!-- Label style -->
<Style x:Key="labelStyle" TargetType="{x:Type Label}">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Margin" Value="0,0,0,5" />
</Style>
我认为这两种声明样式的方法都可以回答你的问题。 在第一个中,没有指定TargetType,但属性名称以“Label”为前缀。在第二个中,为Label对象创建样式。
另一种方法是:
<UserControl.Resources>
<Style x:Key="commonStyle" TargetType="Control">
<Setter Property="FontSize" Value="24"/>
</Style>
<Style BasedOn="{StaticResource commonStyle}" TargetType="ListBox"/>
<Style BasedOn="{StaticResource commonStyle}" TargetType="ComboBox"/>
</UserControl.Resources>
答案 2 :(得分:4)
我想将一个样式应用于Textblock和TextBox,但所选的答案对我没有用,因为Textblock不会从Control继承,在我的情况下我想影响Visibility属性,所以我使用了 FrameworkElement
<Style x:Key="ShowIfRequiredStyle" TargetType="{x:Type FrameworkElement}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ShowIfRequiredStyle, UpdateSourceTrigger=PropertyChanged}" Value="true">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
<TextBlock Style="{StaticResource ResourceKey=ShowIfRequiredStyle}"/>
<TextBox Style="{StaticResource ResourceKey=ShowIfRequiredStyle}"/>
这适用于Visibility属性,因为这两个项都继承自Frameworkelement,并且在那里定义了属性。当然,这对于仅在Control中定义的属性不起作用,您可以搜索层次结构树并尝试查找基类,无论如何我认为这可以帮助某人,因为这是一个顶级搜索结果,并且所选答案有点不完整。
答案 3 :(得分:3)
这个问题有一个替代答案。您可以将TargetType参数完全保留在样式之外,这样就可以将其应用于各种不同的控件,但前提是您在属性名称前加上&#34; Control。&#34;
<Style x:Key="Highlight">
<Setter Property="Control.Foreground" Value="Red"/>
</Style>
显然,这仅适用于基本控件类的属性。如果你试图设置ItemsSource说,它会失败,因为没有Control.ItemsSource
答案 4 :(得分:-2)
我有这个工作
<Style x:Key="HeaderStyleThin" TargetType="{x:Type Border}">
<Setter Property="Background" Value="Black" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background=" Value="Red" />
</Style>
</Style.Resources>
</Style>