0

我正在尝试在添加到 TextBox ControlTemplate 的清除按钮的单击事件上清除 TextBox 的文本:

<Window x:Class="WpfApp1.TextBoxClearing"
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"

    mc:Ignorable="d"
    Title="TextBoxClearing" Height="300" Width="300">
<Window.Resources>
    <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFE2E3EA"/>
    <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FFC5DAED"/>
    <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FFB5CFE7"/>
    <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <ControlTemplate.Resources>
                        <Storyboard x:Key="OnMouseLeftButtonDown1">
                            <!-- Storyboard.TargetProperty="(TextBlock.Text)"  Storyboard.TargetName="PART_ContentHost"-->
                            <StringAnimationUsingKeyFrames Storyboard.TargetProperty="Text" 
                                                           Storyboard.Target="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TextBox}}}" >
                                <DiscreteStringKeyFrame KeyTime="0:0:0.0" Value=""/>
                            </StringAnimationUsingKeyFrames>
                        </Storyboard>
                    </ControlTemplate.Resources>
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <DockPanel>
                            <Button x:Name="MYPART_Clear" DockPanel.Dock="Right">X</Button>
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" RenderTransformOrigin="0.5,0.5" />
                        </DockPanel>
                    </Border>
                    <ControlTemplate.Triggers>

                        <EventTrigger RoutedEvent="UIElement.MouseLeftButtonDown" SourceName="MYPART_Clear">
                            <BeginStoryboard Storyboard="{StaticResource OnMouseLeftButtonDown1}"/>
                        </EventTrigger>
                        <Trigger Property="Text" Value="">
                            <Setter TargetName="MYPART_Clear" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                    <Condition Property="IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
            </MultiTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <TextBox VerticalAlignment="Center" HorizontalAlignment="Center" Width="150" Style="{DynamicResource TextBoxStyle1}"></TextBox>
</Grid>

一切似乎在逻辑上都很好,但我不明白为什么它不能清除文本。我曾尝试通过其名称(PART_ContentHost)访问元素或找到父 TextBox,但它们都处于风向标。

它可以通过纯 XAML 实现吗?我不喜欢安装 Blend SDK。使用这些主要工具应该可以做到这一点。不需要任何 Nuget 安装的自定义 ActionTriger 也可以。

请注意,类似问题的答案需要安装 Nuget,这对我来说并不理想。

4

1 回答 1

2

我在 MSDN 网站上发现了什么

在备注部分下:

此属性不支持动画。

https://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.text.aspx

解决方案(工作)

到目前为止,以下代码正在运行。我将该Tag属性用于要删除的占位符Text

<Window x:Class="TestBase.Window2"
    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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestBase"
    mc:Ignorable="d"
    Title="Window2"
    SizeToContent="WidthAndHeight" d:DesignWidth="236" d:DesignHeight="187"
    MinWidth="300"
    MinHeight="300">
<Window.Resources>
    <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFE2E3EA"/>
    <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FFC5DAED"/>
    <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FFB5CFE7"/>
    <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <DockPanel>
                            <Button x:Name="MYPART_Clear" DockPanel.Dock="Right">X</Button>
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" RenderTransformOrigin="0.5,0.5" />
                        </DockPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="Button.Click" SourceName="MYPART_Clear">
                            <BeginStoryboard Name="FocusTrueStoryboard">
                                <Storyboard>
                                    <StringAnimationUsingKeyFrames 
                                        Storyboard.TargetProperty="(TextBlock.Tag)" 
                                        Storyboard.Target="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TextBox}}}">
                                        <DiscreteStringKeyFrame KeyTime="0:0:0" Value="delete"/>
                                        <DiscreteStringKeyFrame KeyTime="0:0:0.1" Value=""/>
                                    </StringAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                        <Trigger Property="Tag" Value="delete">
                            <Setter Property="Text" Value=""/>
                        </Trigger>
                        <Trigger Property="Text" Value="">
                            <Setter TargetName="MYPART_Clear" Property="Visibility" Value="Collapsed" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                    <Condition Property="IsSelectionActive" Value="false"/>
                </MultiTrigger.Conditions>
                <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
            </MultiTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <TextBox x:Name="asd" VerticalAlignment="Center" HorizontalAlignment="Center" Width="150" Style="{DynamicResource TextBoxStyle1}"></TextBox>
    <TextBlock Text="{Binding ElementName=asd, Path=Tag}" VerticalAlignment="Bottom"/>
</Grid>

预习

预习

于 2017-08-04T06:20:10.710 回答