回复 6楼CanYou1的帖子
你好。
这个问题和之前你提到的C1MaskedTextBox的改变Tooltip背景色的问题有类似的方法,都是不能直接设置的,需要重写模板。
基本思路就是,设置C1MaskedTextBox的ValidationDecoratorStyle="{StaticResource C1ValidationDecoratorStyle1}", 然后重写模板。
重写模板的这部分代码已经实现了,如下所示。
该代码在离开焦点的时候,依然会校验。
以下代码只是针对这个需求,如果在你添加到你的产品中,由于会存在业务逻辑,这部分还需要你们专业的测试验证正确性。
- <Style x:Key="ValidationToolTipStyle" TargetType="{x:Type ToolTip}">
- <Setter Property="Background" Value="Red" />
- <Setter Property="Foreground" Value="Green" />
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type ToolTip}">
- <Grid x:Name="Root" Margin="5 0">
- <Border Background="{TemplateBinding Background}" CornerRadius="2">
- <ContentPresenter x:Name="Content" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" />
- </Border>
- </Grid>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- <!-- C1ValidationDecorator Style-->
- <Style x:Key="C1ValidationDecoratorStyle1" TargetType="{x:Type c1:C1ValidationDecorator}">
- <Setter Property="Background" Value="Red" />
- <Setter Property="Foreground" Value="Green" />
- <Setter Property="CornerRadius" Value="1" />
- <Setter Property="BorderThickness" Value="1" />
- <Setter Property="IsTabStop" Value="False" />
- <Setter Property="IsHitTestVisible" Value="False" />
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type c1:C1ValidationDecorator}">
- <Border x:Name="ValidationErrorElement" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="{TemplateBinding CornerRadius}" BorderBrush="{TemplateBinding Background}" Visibility="Collapsed">
- <VisualStateManager.VisualStateGroups>
- <VisualStateGroup x:Name="ValidationStates">
- <VisualState x:Name="Valid" />
- <VisualState x:Name="InvalidUnfocused">
- <Storyboard>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility">
- <DiscreteObjectKeyFrame KeyTime="0">
- <DiscreteObjectKeyFrame.Value>
- <Visibility>Visible</Visibility>
- </DiscreteObjectKeyFrame.Value>
- </DiscreteObjectKeyFrame>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen">
- <DiscreteObjectKeyFrame KeyTime="0">
- <DiscreteObjectKeyFrame.Value>
- <System:Boolean>True</System:Boolean>
- </DiscreteObjectKeyFrame.Value>
- </DiscreteObjectKeyFrame>
- </ObjectAnimationUsingKeyFrames>
- </Storyboard>
- </VisualState>
- <VisualState x:Name="InvalidFocused">
- <Storyboard>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility">
- <DiscreteObjectKeyFrame KeyTime="0">
- <DiscreteObjectKeyFrame.Value>
- <Visibility>Visible</Visibility>
- </DiscreteObjectKeyFrame.Value>
- </DiscreteObjectKeyFrame>
- </ObjectAnimationUsingKeyFrames>
- <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen">
- <DiscreteObjectKeyFrame KeyTime="0">
- <DiscreteObjectKeyFrame.Value>
- <System:Boolean>True</System:Boolean>
- </DiscreteObjectKeyFrame.Value>
- </DiscreteObjectKeyFrame>
- </ObjectAnimationUsingKeyFrames>
- </Storyboard>
- </VisualState>
- </VisualStateGroup>
- </VisualStateManager.VisualStateGroups>
- <ToolTipService.ToolTip>
- <ToolTip x:Name="validationTooltip" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Placement="Right" PlacementTarget="{TemplateBinding Target}" Style="{StaticResource ValidationToolTipStyle}" Content="{Binding Target.(Validation.Errors), RelativeSource={RelativeSource TemplatedParent}}">
- <ToolTip.ContentTemplate>
- <DataTemplate>
- <TextBlock Margin="8 4" MaxWidth="250" VerticalAlignment="Center" Text="{Binding [0].ErrorContent}" TextWrapping="Wrap" />
- </DataTemplate>
- </ToolTip.ContentTemplate>
- </ToolTip>
- </ToolTipService.ToolTip>
- <Path Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z" Fill="{TemplateBinding Background}" Margin="0 -1 -1 0" HorizontalAlignment="Right" VerticalAlignment="Top" />
- </Border>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
复制代码 |