IntegerUpDown控件在关注文本框时更改背景

时间:2019-01-16 00:01:22

标签: c# wpf xaml mvvm styles

我正在创建WPF应用程序,并且正在使用WPF Extended Toolkit库。我已经将IntegerUpDown控件添加到了UserControl中,当用户在文本框内按下时,我希望背景颜色从深灰色变为浅灰色。

我尝试在xaml中添加样式触发器,该触发器将在IntegerUpDown控件被IsFocused更改背景时触发。但是,这似乎不起作用。

DerivedB

通过添加样式,我希望控件的背景从深灰色变为浅灰色,但是什么也没有发生。我该如何做到这一点?

3 个答案:

答案 0 :(得分:0)

我在自己的应用中尝试了此问题,并已完成。代码如下:

<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 Animation dependency issue

答案 1 :(得分:0)

IntegerUpDown源代码中的触发器相同,因此外部触发器不再有效。

IntegerUpDown source code:

<Trigger Property="IsFocused" Value="True">
    <Setter TargetName="PART_TextBox"
          Property="FocusManager.FocusedElement"
          Value="{Binding ElementName=PART_TextBox}" />
</Trigger>

我尝试使用GotFocus和LostFocus事件。

xaml:

<xctk:IntegerUpDown x:Name="Day" 
     LostFocus="IntegerUpDown_LostFocus" 
     GotFocus="IntegerUpDown_GotFocus"  
     Focusable="True"  
     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>

cs代码:

private void IntegerUpDown_GotFocus(object sender, RoutedEventArgs e)
{
    Day.Background = new SolidColorBrush(Colors.Gray);
}

private void IntegerUpDown_LostFocus(object sender, RoutedEventArgs e)
{
    Day.Background = new SolidColorBrush(Colors.DarkGray);
}

答案 2 :(得分:0)

看到@ J.B.D的回答后,我为IntegerUpDown控件编辑了ControlTemplate来改变了背景

<ControlTemplate x:Key="ControlControlTemplate1" TargetType="{x:Type Control}">
            <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">
                <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}}">
                    <xctk:WatermarkTextBox.Style>
                        <Style TargetType="{x:Type xctk:WatermarkTextBox}">
                            <Setter Property="Background" Value="{StaticResource DarkGreyBrush}" />
                            <Style.Triggers>
                                <Trigger Property="IsFocused" Value="True">
                                    <Setter Property="Background" Value="{StaticResource LightGreyBrush}" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </xctk:WatermarkTextBox.Style>
                </xctk:WatermarkTextBox>

            </xctk:ButtonSpinner>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BorderBrush" Value="{DynamicResource {ComponentResourceKey ResourceId=ControlMouseOverBorderKey, TypeInTargetAssembly={x:Type Themes:ResourceKeys}}}"/>
                </Trigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="False"/>
                        <Condition Binding="{Binding AllowTextInput, RelativeSource={RelativeSource Self}}" Value="False"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="IsReadOnly" TargetName="PART_TextBox" Value="True"/>
                </MultiDataTrigger>
                <DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource Self}}" Value="True">
                    <Setter Property="IsReadOnly" TargetName="PART_TextBox" Value="True"/>
                </DataTrigger>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                    <Setter Property="BorderBrush" Value="{DynamicResource {ComponentResourceKey ResourceId=ControlSelectedBorderKey, TypeInTargetAssembly={x:Type Themes:ResourceKeys}}}"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="FocusManager.FocusedElement" TargetName="PART_TextBox" Value="{Binding ElementName=PART_TextBox}"/>
                    <Setter TargetName="PART_TextBox" Property="Visibility" Value="Hidden" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>

请查看控件模板和WatermarkTextBox的开头。我添加了WatermarkTextBox.Style来使文本框聚焦时改变背景。

要覆盖COntrolTemplate,请右键单击IntegerUpDown控件,然后按Edit Template