首页 > 解决方案 > 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>

通过我添加的样式,我希望控件的背景从深灰色变为浅灰色,但没有发生任何事情。我怎样才能做到这一点?

标签: c#wpfxamlmvvmstyles

解决方案


我在自己的应用程序中尝试了这个问题,它已经完成了。这是代码:

<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 动画依赖问题


推荐阅读