我试图在添加到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。这应该可以使用这些主要工具。 另外自定义ActionTriger也不需要任何Nuget安装。
请注意类似问题的答案需要Nuget安装,这对我来说是不可取的。
答案 0 :(得分:1)
我在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>