我有以下ControlTemplate和样式。 文本框如下所示。
<TextBox Style="{StaticResource TextBoxRoundedCornerStyle}">
我希望的工具提示列出文本框控件的所有验证错误。有人可以帮忙,告诉我我在做什么错吗? 我可能绑定有误,但是我无法弄清楚。
下面的控件模板和样式都位于单独的资源文件中。验证模板的样式为空的原因是在对边框进行动画处理时,否则我会得到两个红色边框,而当我在controltemplate中制作动画时,验证器的更新似乎只能正常工作。
<ControlTemplate x:Key="TextBoxBaseControlTemplateMainScreen" TargetType="{x:Type TextBoxBase}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource MainScreen.TextBox.MouseOver.Background}" SnapsToDevicePixels="True" CornerRadius="3">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
<Grid x:Name="theGrid" Margin="1,0,2,0" Visibility="Collapsed">
<Viewbox Stretch="Uniform" Width="12" Height="12"
HorizontalAlignment="Left"
VerticalAlignment="Center" >
<icons:Failure>
<icons:Failure.ToolTip>
<ItemsControl>
<ItemsControl.ItemsSource>
<Binding RelativeSource="{RelativeSource Self}" Path="(Validation.Errors)"/>
</ItemsControl.ItemsSource>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Foreground="Red" FontSize="12"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</icons:Failure.ToolTip>
</icons:Failure>
</Viewbox>
</Grid>
<AdornedElementPlaceholder Margin="0" x:Name="adorner" />
</StackPanel>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid">
</VisualState>
<VisualState x:Name="InvalidFocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MoveTextBox.Invalid.BorderBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="theGrid">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="theGrid" RepeatBehavior="3x">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.28" Value="0.2"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="InvalidUnfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MoveTextBox.Invalid.BorderBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="theGrid">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="theGrid" RepeatBehavior="3x">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.2"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
<ControlTemplate.Triggers>
<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 MainScreen.TextBox.MouseOver.BorderBrush}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource MainScreen.TextBox.MouseOver.BorderBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
以及文本框使用的样式。
<Style x:Key="TextBoxRoundedCornerStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template" Value="{StaticResource TextBoxBaseControlTemplateMainScreen}"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<ControlTemplate.Triggers>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:0)
我使用以下代码解决了它。 现在唯一的问题是,有时Validation.Errors是单个字符串,有时是字符串列表。我不确定如何妥善处理这两种情况。我将对此发布一个单独的问题。
<ControlTemplate x:Key="TextBoxBaseControlTemplateMainScreen" TargetType="{x:Type TextBoxBase}">
<Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{StaticResource MainScreen.TextBox.MouseOver.Background}" SnapsToDevicePixels="True" CornerRadius="3">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
<Grid x:Name="theGrid" Margin="1,0,2,0" Visibility="Collapsed">
<Grid.ToolTip>
<ItemsControl
x:Name="ErrorDisplay"
ItemsSource="{TemplateBinding Validation.Errors}"
DisplayMemberPath="ErrorContent[0]"
Foreground="Red"
FontSize="12">
</ItemsControl>
</Grid.ToolTip>
<Viewbox Stretch="Uniform" Width="12" Height="12"
HorizontalAlignment="Left"
VerticalAlignment="Center" >
<icons:Failure/>
</Viewbox>
</Grid>
<AdornedElementPlaceholder Margin="0" x:Name="adorner" />
</StackPanel>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ValidationStates">
<VisualState x:Name="Valid">
</VisualState>
<VisualState x:Name="InvalidFocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MoveTextBox.Invalid.BorderBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="theGrid">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="theGrid" RepeatBehavior="3x">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.28" Value="0.2"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="InvalidUnfocused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Border.BorderBrush)" Storyboard.TargetName="border">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource MoveTextBox.Invalid.BorderBrush}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="theGrid">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="theGrid" RepeatBehavior="3x">
<EasingDoubleKeyFrame KeyTime="0" Value="1"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0.2"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
<ControlTemplate.Triggers>
<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 MainScreen.TextBox.MouseOver.BorderBrush}"/>
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource MainScreen.TextBox.MouseOver.BorderBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="TextBoxRoundedCornerStyle" TargetType="{x:Type TextBox}">
<Setter Property="Template" Value="{StaticResource TextBoxBaseControlTemplateMainScreen}"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<ControlTemplate.Triggers>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>