TextBox EventTrigger 未设置 属性

TextBox EventTrigger not setting the Property

我正在尝试在添加到 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) 或找到父文本框,但所有这些都处于风向标。

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

请注意,类似问题的回答需要安装 Nuget,这不是我想要的。

我在 MSDN 站点上找到的内容

在备注部分下:

This property does not support animation.

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>

预览