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>

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


慕桂英3389331
浏览 196回答 3
3回答

陪伴而非守候

我在自己的应用程序中尝试了这个问题,它已经完成了。这是代码:<Windowx: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>&nbsp; &nbsp; <Style x:Key="IntegerUpDownStyle" TargetType="xctk:IntegerUpDown">&nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <EventTrigger RoutedEvent="ValueChanged">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <EventTrigger.Actions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <BeginStoryboard>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Storyboard>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ColorAnimation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Storyboard.TargetProperty="Background.Color"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; From="DarkGray"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; To="Transparent"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Duration="0:0:1" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Storyboard>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </BeginStoryboard>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </EventTrigger.Actions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </EventTrigger>&nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; </Style></Window.Resources><StackPanel>&nbsp; &nbsp; <xctk:IntegerUpDown Width="200" Style="{StaticResource IntegerUpDownStyle}">&nbsp; &nbsp; &nbsp; &nbsp; <xctk:IntegerUpDown.Background>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SolidColorBrush Color="Transparent" />&nbsp; &nbsp; &nbsp; &nbsp; </xctk:IntegerUpDown.Background>&nbsp; &nbsp; </xctk:IntegerUpDown></StackPanel>

万千封印

IntegerUpDown 源代码中的相同触发器,因此外部触发器不再有效。IntegerUpDown 源代码:<Trigger Property="IsFocused" Value="True">&nbsp; &nbsp; <Setter TargetName="PART_TextBox"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Property="FocusManager.FocusedElement"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value="{Binding ElementName=PART_TextBox}" /></Trigger>我尝试使用 GotFocus 和 LostFocus 事件。xml:<xctk:IntegerUpDown x:Name="Day"&nbsp;&nbsp; &nbsp; &nbsp;LostFocus="IntegerUpDown_LostFocus"&nbsp;&nbsp; &nbsp; &nbsp;GotFocus="IntegerUpDown_GotFocus"&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;Focusable="True"&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;Value="{Binding DayText, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Style="{StaticResource IntegerUpDownStyle}" Minimum="{Binding MinimumDateSelection}" Maximum="{Binding MaximumDateSelection}">&nbsp; &nbsp;<xctk:IntegerUpDown.Watermark>&nbsp; &nbsp; &nbsp; &nbsp; <TextBlock Text="Day" Foreground="{StaticResource OffsetWhiteBrush}" Margin="0,0,60,0"/>&nbsp; &nbsp; </xctk:IntegerUpDown.Watermark></xctk:IntegerUpDown>cs代码:private void IntegerUpDown_GotFocus(object sender, RoutedEventArgs e){&nbsp; &nbsp; Day.Background = new SolidColorBrush(Colors.Gray);}private void IntegerUpDown_LostFocus(object sender, RoutedEventArgs e){&nbsp; &nbsp; Day.Background = new SolidColorBrush(Colors.DarkGray);}

DIEA

在看到@JBD 的答案后,我编辑了 IntegerUpDown 控件的 ControlTemplate 以更改背面<ControlTemplate x:Key="ControlControlTemplate1" TargetType="{x:Type Control}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xctk:ButtonSpinner x:Name="PART_Spinner" AllowSpin="{Binding AllowSpin, RelativeSource={RelativeSource TemplatedParent}}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ButtonSpinnerLocation="{Binding ButtonSpinnerLocation, RelativeSource={RelativeSource TemplatedParent}}" Background="{TemplateBinding Background}" HorizontalContentAlignment="Stretch" IsTabStop="False" ShowButtonSpinner="{Binding ShowButtonSpinner, RelativeSource={RelativeSource TemplatedParent}}" VerticalContentAlignment="Stretch">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xctk:WatermarkTextBox x:Name="PART_TextBox" AutoMoveFocus="{Binding AutoMoveFocus, RelativeSource={RelativeSource TemplatedParent}}" AutoSelectBehavior="{Binding AutoSelectBehavior, RelativeSource={RelativeSource TemplatedParent}}" AcceptsReturn="False" BorderThickness="0" ContextMenu="{TemplateBinding ContextMenu}" Foreground="{TemplateBinding Foreground}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontStretch="{TemplateBinding FontStretch}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsTabStop="True" IsUndoEnabled="True" MinWidth="20" MaxLength="{Binding MaxLength, RelativeSource={RelativeSource TemplatedParent}}" Padding="{TemplateBinding Padding}" TextAlignment="{Binding TextAlignment, RelativeSource={RelativeSource TemplatedParent}}" TextWrapping="NoWrap" TabIndex="{TemplateBinding TabIndex}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" WatermarkTemplate="{Binding WatermarkTemplate, RelativeSource={RelativeSource TemplatedParent}}" Watermark="{Binding Watermark, RelativeSource={RelativeSource TemplatedParent}}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xctk:WatermarkTextBox.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="{x:Type xctk:WatermarkTextBox}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="{StaticResource DarkGreyBrush}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="IsFocused" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Background" Value="{StaticResource LightGreyBrush}" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xctk:WatermarkTextBox.Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xctk:WatermarkTextBox>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </xctk:ButtonSpinner>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ControlTemplate.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="IsMouseOver" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="BorderBrush" Value="{DynamicResource {ComponentResourceKey ResourceId=ControlMouseOverBorderKey, TypeInTargetAssembly={x:Type Themes:ResourceKeys}}}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <MultiDataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <MultiDataTrigger.Conditions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Condition Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="False"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Condition Binding="{Binding AllowTextInput, RelativeSource={RelativeSource Self}}" Value="False"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </MultiDataTrigger.Conditions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="IsReadOnly" TargetName="PART_TextBox" Value="True"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </MultiDataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="IsReadOnly" TargetName="PART_TextBox" Value="True"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTrigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="IsKeyboardFocusWithin" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="BorderBrush" Value="{DynamicResource {ComponentResourceKey ResourceId=ControlSelectedBorderKey, TypeInTargetAssembly={x:Type Themes:ResourceKeys}}}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="IsEnabled" Value="False">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Trigger Property="IsFocused" Value="True">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="FocusManager.FocusedElement" TargetName="PART_TextBox" Value="{Binding ElementName=PART_TextBox}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter TargetName="PART_TextBox" Property="Visibility" Value="Hidden" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Trigger>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ControlTemplate.Triggers>&nbsp; &nbsp; &nbsp; &nbsp; </ControlTemplate>请查看控件模板的开头和 WatermarkTextBox。WatermarkTextBox.Style 是我添加的,用于在文本框获得焦点时更改背景。要覆盖控制模板,请右键单击 IntegerUpDown 控件,然后按编辑模板
打开App,查看更多内容
随时随地看视频慕课网APP