亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

IntegerUpDown 控件在 Textbox IsFocused 時更改背景

IntegerUpDown 控件在 Textbox IsFocused 時更改背景

C#
慕桂英3389331 2022-07-23 17:08:51
我正在創建一個 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>通過我添加的樣式,我希望控件的背景從深灰色變為淺灰色,但沒有發生任何事情。我怎樣才能做到這一點?
查看完整描述

3 回答

?
陪伴而非守候

TA貢獻1757條經驗 獲得超8個贊

我在自己的應用程序中嘗試了這個問題,它已經完成了。這是代碼:


<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>


查看完整回答
反對 回復 2022-07-23
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

IntegerUpDown 源代碼中的相同觸發器,因此外部觸發器不再有效。


IntegerUpDown 源代碼:


<Trigger Property="IsFocused" Value="True">

    <Setter TargetName="PART_TextBox"

          Property="FocusManager.FocusedElement"

          Value="{Binding ElementName=PART_TextBox}" />

</Trigger>

我嘗試使用 GotFocus 和 LostFocus 事件。


xml:


<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);

}


查看完整回答
反對 回復 2022-07-23
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

在看到@JBD 的答案后,我編輯了 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 是我添加的,用于在文本框獲得焦點時更改背景。


要覆蓋控制模板,請右鍵單擊 IntegerUpDown 控件,然后按編輯模板


查看完整回答
反對 回復 2022-07-23
  • 3 回答
  • 0 關注
  • 224 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號