c# - IntegerUpDown 控件在 Textbox IsFocused 时更改背景
问题描述
我正在创建一个 WPF 应用程序并使用 WPF 扩展工具包库。我已将 IntegerUpDown 控件添加到我的 UserControl 中,当用户在文本框内按下时,我希望背景颜色从深灰色变为浅灰色。
我尝试在 xaml 中添加一个样式触发器,该触发器在 IntegerUpDown 控件 IsFocused 用于背景更改时触发。但是,这似乎不起作用。
<xctk:IntegerUpDown x:Name="Day" Value="{Binding DayText, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Style="{StaticResource IntegerUpDownStyle}" Minimum="{Binding MinimumDateSelection}" Maximum="{Binding MaximumDateSelection}">
<xctk:IntegerUpDown.Watermark>
<TextBlock Text="Day" Foreground="{StaticResource OffsetWhiteBrush}" Margin="0,0,60,0"/>
</xctk:IntegerUpDown.Watermark>
</xctk:IntegerUpDown>
<!-- Textbox and PasswordBox base styling for login boxes -->
<Style x:Key="IntegerUpDownStyle" TargetType="{x:Type Control}" BasedOn="{StaticResource BaseTextStyle}">
<Setter Property="MaxWidth" Value="400" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FontSize" Value="{StaticResource FontSize20}" />
<Setter Property="FontFamily" Value="{StaticResource LatoRegular}" />
<Setter Property="Background" Value="{StaticResource DarkGreyBrush}" />
<Setter Property="Margin" Value="0,20,0,0" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="True">
<Setter Property="Background" Value="{StaticResource LightGreyBrush}" />
</Trigger>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
通过我添加的样式,我希望控件的背景从深灰色变为浅灰色,但没有发生任何事情。我怎样才能做到这一点?
解决方案
我在自己的应用程序中尝试了这个问题,它已经完成了。这是代码:
<Window
x:Class="WpfApp16.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp16"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.Resources>
<Style x:Key="IntegerUpDownStyle" TargetType="xctk:IntegerUpDown">
<Style.Triggers>
<EventTrigger RoutedEvent="ValueChanged">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Background.Color"
From="DarkGray"
To="Transparent"
Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<xctk:IntegerUpDown Width="200" Style="{StaticResource IntegerUpDownStyle}">
<xctk:IntegerUpDown.Background>
<SolidColorBrush Color="Transparent" />
</xctk:IntegerUpDown.Background>
</xctk:IntegerUpDown>
</StackPanel>
有关更多信息,请查看此链接:WPF 动画依赖问题
推荐阅读
- javascript - 在显示模式窗口时阻止其他页面元素的功能
- node.js - Neo4j 无法读取未定义的属性“驱动程序”
- mysql - 如何在我的 sql 中的一个查询中连接两列
- python-3.x - 将 Python 代码部署到 Google 云,以更新云 sql 中的表以用于 mysql
- kubernetes - 即使在 pod 重新启动后,如何在 kubernetes 的 pod 中的文件中保持更改完成?
- oracle - 拒绝访问域时如何使用 startComponent.cmd 启动 oracle 报表服务器
- php - laravel,如何向函数 __construct() 添加条件
- curl - curl命令出错(curl:(35)读取X.509可能加密的密钥文件时出错:解析时出错。)
- python - 使用 Pandas 计算累积平均值
- mysql - 来自 MySQL 的数据透视表