当 IsPress=True 时按钮控件样式改变背景,但它永远不会变回原来的颜色
Button control style changes the background when IsPress=True, but it never changes back to the original color
我在 XAML 资源字典中定义了一个应用程序默认按钮样式。 ControlTemplate
包含 IsPressed="True"
的触发器,它会在按下按钮时更改按钮背景。
但是,按下并释放按钮后,IsPressed="True"
期间设置的背景颜色永远不会变回原来的背景颜色。
这是在 VS 2019 和 .NET core v3.1 中。
按钮xaml是:
<Button x:Name="btnHelp" Width="50" Height="25" Content="_Help" Click="btnHelp_Click" BorderBrush="AliceBlue" Margin="10,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
这是我的资源字典中的按钮样式定义:
<Style TargetType="{x:Type Button}">
<Setter Property="Padding" Value="1" />
<Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource BaseBorderBrush}" />
<Setter Property="BorderThickness" Value="{DynamicResource ButtonBorderThickness}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border
x:Name="outerborder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
ClipToBounds="True"
CornerRadius="{DynamicResource ButtonCornerRadius}">
<Grid>
<Border
Name="glow"
Margin="{DynamicResource GlowBorderMargin}"
BorderBrush="{DynamicResource ButtonGlowBrush}"
BorderThickness="{DynamicResource GlowBorderThickness}"
CornerRadius="{DynamicResource ButtonCornerRadius}"
Effect="{DynamicResource ButtonHoverGlowEffect}"
Visibility="Collapsed" />
<ContentPresenter
x:Name="content"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
<Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />
<!--<Setter TargetName="glow" Property="Visibility" Value="Visible" />-->
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundPressedBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource DarkerSelectedBrush}" />
<Setter TargetName="glow" Property="Effect" Value="{DynamicResource ButtonPressedGlowEffect}" />
<Setter TargetName="glow" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="IsFocused" Value="True" >
<Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
<Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Transparent" />
<Setter TargetName="outerborder" Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{DynamicResource DisabledBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
罪魁祸首是 IsFocused
触发器中的下面一行。当您单击 Button
时,它会获得焦点,因此触发器将设置按钮的 Background
。如果您将焦点移动到另一个控件,例如按 Tab,背景将变回正常。由于您对 Pressed 和 Focused 状态使用相同的背景,因此 看起来 卡在按下状态,但是专注。
<Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />
从触发器中删除线,您的按钮将按预期工作。
<Trigger Property="IsFocused" Value="True" >
<Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
</Trigger>
我在 XAML 资源字典中定义了一个应用程序默认按钮样式。 ControlTemplate
包含 IsPressed="True"
的触发器,它会在按下按钮时更改按钮背景。
但是,按下并释放按钮后,IsPressed="True"
期间设置的背景颜色永远不会变回原来的背景颜色。
这是在 VS 2019 和 .NET core v3.1 中。
按钮xaml是:
<Button x:Name="btnHelp" Width="50" Height="25" Content="_Help" Click="btnHelp_Click" BorderBrush="AliceBlue" Margin="10,0,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
这是我的资源字典中的按钮样式定义:
<Style TargetType="{x:Type Button}">
<Setter Property="Padding" Value="1" />
<Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource BaseBorderBrush}" />
<Setter Property="BorderThickness" Value="{DynamicResource ButtonBorderThickness}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border
x:Name="outerborder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
ClipToBounds="True"
CornerRadius="{DynamicResource ButtonCornerRadius}">
<Grid>
<Border
Name="glow"
Margin="{DynamicResource GlowBorderMargin}"
BorderBrush="{DynamicResource ButtonGlowBrush}"
BorderThickness="{DynamicResource GlowBorderThickness}"
CornerRadius="{DynamicResource ButtonCornerRadius}"
Effect="{DynamicResource ButtonHoverGlowEffect}"
Visibility="Collapsed" />
<ContentPresenter
x:Name="content"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
<Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />
<!--<Setter TargetName="glow" Property="Visibility" Value="Visible" />-->
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="{DynamicResource ButtonBackgroundPressedBrush}" />
<Setter Property="BorderBrush" Value="{DynamicResource DarkerSelectedBrush}" />
<Setter TargetName="glow" Property="Effect" Value="{DynamicResource ButtonPressedGlowEffect}" />
<Setter TargetName="glow" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="IsFocused" Value="True" >
<Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
<Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Transparent" />
<Setter TargetName="outerborder" Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{DynamicResource DisabledBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
罪魁祸首是 IsFocused
触发器中的下面一行。当您单击 Button
时,它会获得焦点,因此触发器将设置按钮的 Background
。如果您将焦点移动到另一个控件,例如按 Tab,背景将变回正常。由于您对 Pressed 和 Focused 状态使用相同的背景,因此 看起来 卡在按下状态,但是专注。
<Setter Property="Background" Value="{DynamicResource MenuHoverBrush}" />
从触发器中删除线,您的按钮将按预期工作。
<Trigger Property="IsFocused" Value="True" >
<Setter Property="BorderBrush" Value="{DynamicResource SelectedBrush}" />
</Trigger>