所以基本上我想实现悬停效果,效果是文本框底部会出现一条线,这条线的宽度必须是文本框实际宽度的一半.

如果可能的话,我想要一个动画,也许是一个彩色动画,它的 colored颜色 会逐渐从透明变成纯色.我还想指定我的TextBox是一个带水印的TextBox.

我想要实现的一个例子是: EXAMPLE

<Style TargetType="{x:Type local:TextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:TextBox}">
                <DockPanel HorizontalAlignment="Stretch">
                    <Border CornerRadius="10" BorderBrush="{StaticResource BackgroundColor}" BorderThickness="1" Background="{StaticResource BackgroundColor}">
                        <Grid Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}">
                            <TextBox Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}" VerticalAlignment="Center" Foreground="{StaticResource TextColor}" Background="{StaticResource BackgroundColor}" Padding="10" HorizontalAlignment="Left" x:Name="SearchTermTextBox" Margin="5" BorderThickness="0"/>
                                <TextBlock IsHitTestVisible="False" Text="Enter Search Term Here" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="20,0,0,0" Foreground="{StaticResource TextColor}" Background="{StaticResource BackgroundColor}">
                                    <TextBlock.Style>
                                        <Style TargetType="{x:Type TextBlock}">
                                            <Setter Property="Visibility" Value="Collapsed"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Text, ElementName=SearchTermTextBox}" Value="">
                                                    <Setter Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>
                        </Grid>         
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

推荐答案

TextBoxControl,因此可以像任何其他模板一样重新模板化.诀窍是确保有一个名为PART_ContentHost的子元素-通常是ScrollViewer-控件将使用它来呈现可编辑的文本.(有关详细信息,请参阅WPF Default Styles and Templates documentation for TextBox).

这里有一个简单的例子,它将在悬停时显示一条下划线,淡入/淡出0.25秒,并将占据TextBox‘S宽度的50%(这要归功于一些Grid列逻辑).你应该能够进一步调整它,以获得你想要的外观/行为.别忘了,你也可以在TextBox的子类中添加更多的DependencyProperty,以获得外观上的额外粒度.

<Style x:Key="ResponsiveTextBoxStyle"
       TargetType="TextBox"
       BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="BorderBrush"
            Value="Blue" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="Underline"
                                                     Storyboard.TargetProperty="(Border.Opacity)"
                                                     To="0"
                                                     Duration="0:00:00.25" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="MouseOver">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="Underline"
                                                     Storyboard.TargetProperty="(Border.Opacity)"
                                                     To="1"
                                                     Duration="0:00:00.25" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ScrollViewer Margin="0"
                                  Grid.Row="0"
                                  Grid.ColumnSpan="4"
                                  x:Name="PART_ContentHost" />
                    <Border x:Name="Underline"
                            Grid.Row="1"
                            Opacity="0"
                            BorderThickness="1"
                            Height="2"
                            Grid.Column="1"
                            Grid.ColumnSpan="2"
                            BorderBrush="{TemplateBinding BorderBrush}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Csharp相关问答推荐

在WPF.NET 6中使用C++/WinRT组件(但实际上是任何WinRT组件)

IComponition.获取IReadOnlyCollection的返回默认属性值

为什么我不能更改尚未设置的模拟对象属性的值?

如何阻止注释被包含在C#release build. exe中

在FilePath中搜索一个词,并根据First Match从左到右提取文件路径

碰撞检测与盒碰撞器,其isTrigger on问题

C#XmlSerializer-输出控制新行的多个XML片段

如何在Windows 11任务调度程序中每1分钟重复一次任务?

需要在重新启动ApplicartionPool或IIS后启动/唤醒API的帮助

为具有实体框架后端的Reaction项目 Select 正确的Visual Studio模板

WinForms在Linux上的JetBrains Rider中的应用

使用泛型可空类实现接口

使用带有参数和曲面的注入失败(&Q;)

如何比较C#中的L和ł(波兰字符)返回TRUE

TagHelpers在新区域不起作用

在C#ASP.NET内核中使用INT AS-1进行控制器场景的单元测试

如何使用IHostedService添加数据种子方法

当我在Git中暂存文件更改时,它们会消失

从具有泛型类型约束的类继承-Blazor

MS Project读取项目自定义域