gpt4 book ai didi

IsEnabled = False 时的 WPF 按钮背景

转载 作者:行者123 更新时间:2023-12-03 23:51:32 26 4
gpt4 key购买 nike

我尝试在按钮未启用时为其设置样式,以便背景 LinearGradientBrush 与启用时为按钮设置的样式相似。我尝试过使用触发器,但似乎只有文本颜色在改变:

<Application x:Class="RSPolar.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!--Windows style-->
<Style TargetType="Window">

<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.92" />
<GradientStop Color="Gray" Offset="0.9" />
<GradientStop Color="LightGray" Offset="0.88" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
<!--Buttons style-->
<Style TargetType="Button">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="48" Foreground="#00FF00" Text="{Binding Path=Content,RelativeSource={RelativeSource AncestorType={x:Type Button}}}">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="1" Color="DarkGray" ShadowDepth="1" Direction="120"/>
</TextBlock.Effect>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.52" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="LightGray" Offset="0.48" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Button.Effect">
<Setter.Value>
<DropShadowEffect Color="Black" Direction="320" ShadowDepth="5" BlurRadius="10" Opacity="0.5" />
</Setter.Value>
</Setter>
<Setter Property="Button.Effect">
<Setter.Value>
<BlurEffect Radius="1"></BlurEffect>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock FontWeight="Bold" FontSize="48" Foreground="#FF0000" Text="{Binding Path=Content,RelativeSource={RelativeSource AncestorType={x:Type Button}}}">
<TextBlock.Effect>
<DropShadowEffect BlurRadius="1" Color="DarkGray" ShadowDepth="1" Direction="120"/>
</TextBlock.Effect>
</TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.52" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="LightGray" Offset="0.48" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Application.Resources>
</Application>

最佳答案

您不应该定义 ContentTemplate 属性。如果您想更改按钮的外观,您应该定义 Template 属性并将您的 Trigger 添加到您在那里定义的 ControlTemplate 中.

如果您阅读 Customizing the Appearance of an Existing Control by Creating a ControlTemplate 可能会有所帮助MSDN 上的页面?您还可以在 ControlTemplate Class 中找到使用 ControlTemplate.Triggers 的示例。 MSDN 上的页面。

不过,这里是一个简单的例子:

资源:

<ControlTemplate x:Key="ExampleButton" TargetType="{x:Type Button}">
<Border Background="LightGreen" BorderBrush="Black" BorderThickness="1"
CornerRadius="5" x:Name="Border" Padding="10">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background" Value="LightBlue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

在 XAML 页面中:

<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Template="{StaticResource ExampleButton}" Content="Click me" />
<Button Template="{StaticResource ExampleButton}" Content="Click me"
IsEnabled="False" />
</StackPanel>

更新>>>

您只需像在 您的 示例中那样为您的 LinearGradientBrush 替换 'plain' Background 值:

资源:

<ControlTemplate x:Key="ExampleButton" TargetType="{x:Type Button}">
<Border BorderBrush="Black" BorderThickness="1"
CornerRadius="5" x:Name="Border" Padding="10">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.52" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="LightGray" Offset="0.48" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Border.Background>
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="Border" Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Color="Gray" Offset="1" />
<GradientStop Color="DarkGray" Offset="0.52" />
<GradientStop Color="Gray" Offset="0.5" />
<GradientStop Color="LightGray" Offset="0.48" />
<GradientStop Color="WhiteSmoke" Offset="0" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>

Why is Target Border setting the color of the Background of the button? Shouldn't it change the color of the border?

ButtonBackground BorderBackground

关于IsEnabled = False 时的 WPF 按钮背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28877550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com