我在实现装饰器时遇到了麻烦,因为它在 Paya 的回答中,所以我展示了如何将它包装在一个完整的、现成的样式资源中,该资源可以应用于任何标签,它将显示玻璃效果,并将禁用时也会使标签变暗并保留对齐、边框等:
<Style x:Key="GlassLabelStyle" TargetType="{x:Type Label}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
<Grid>
<Decorator>
<Decorator.Effect>
<DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" />
</Decorator.Effect>
<Decorator>
<Decorator.Effect>
<DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" />
</Decorator.Effect>
<Decorator>
<Decorator.Effect>
<DropShadowEffect BlurRadius="7" Color="White" ShadowDepth="0" />
</Decorator.Effect>
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
</ContentPresenter>
</Decorator>
</Decorator>
</Decorator>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
<Setter Property="Opacity" Value="0.5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果样式在您的窗口或应用程序资源中,那么您可以像这样应用它:
<Label Style="{StaticResource GlassLabelStyle}"
当我这样做时,我也遇到了 TextBox 的问题,当控件被禁用时,您根本无法更改背景颜色(它只是一直恢复为白色),因此有人发现您必须覆盖整个模板!请参阅(https://stackoverflow.com/a/3752517/88409)。所以这里有一个现成的样式,它会在禁用时使文本框半透明(在玻璃上看起来很棒),并在启用时使其背景成为半透明的白色,边框更明显:
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="#01000000" />
<SolidColorBrush x:Key="DisabledBorderBrush" Color="#40000000" />
<SolidColorBrush x:Key="DisabledForegroundBrush" Color="#88ffffff" />
<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#88ffffff"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border Name="Bd" BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="true">
<ScrollViewer Name="PART_ContentHost" Background="{TemplateBinding Background}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Value="{StaticResource DisabledBackgroundBrush}" Property="Background" />
<Setter Value="{StaticResource DisabledBorderBrush}" Property="BorderBrush"/>
<Setter Value="{StaticResource DisabledForegroundBrush}" Property="Foreground" />
<Setter TargetName="PART_ContentHost" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
<Setter TargetName="PART_ContentHost" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>