为什么我不能改变ValidationTemplate Style wpf?

时间:2017-04-25 06:40:54

标签: wpf validation

<ControlTemplate x:Key="validationTest">
   <DockPanel LastChildFill="true">
     <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
       <Border BorderBrush="red" BorderThickness="1" />
      </AdornedElementPlaceholder>
    </DockPanel>
</ControlTemplate>...............

<TextBox x:Name="txtbox_tel" Grid.Row="3" Grid.Column="1" 
  Style="{DynamicResource TextInputStyle}" 
  Validation.Error="Validation_Error" 
  Validation.ErrorTemplate="{StaticResource validationTest}
  Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=Tel, 
  ValidatesOnDataErrors=true, NotifyOnValidationError=true}"/>

我使用Validation.Error。

我知道我可以更改验证样式,所以我将ControlTemplate borderBrush颜色修改为绿色,但它没有改变。

但是,检查验证效果很好。

显示红色Tooltip。那个环境在哪里?我无法找到。

如何更改验证Errortemplate样式?请帮帮我......

(我申请了Metro Theme。)

  

添加我的用户界面:

UI look like this

1 个答案:

答案 0 :(得分:1)

如果您只想更改颜色,可以通过使用相同的键定义自己的颜色来覆盖验证画笔:

<Window.Resources>
    <SolidColorBrush x:Key="ValidationBrush1" Color="Green" />
    <SolidColorBrush x:Key="ValidationBrush2" Color="Green" />
    <SolidColorBrush x:Key="ValidationBrush3" Color="Green" />
    <SolidColorBrush x:Key="ValidationBrush4" Color="Green" />
    <SolidColorBrush x:Key="ValidationBrush5" Color="Green" />
</Window.Resources>

另一种选择是重新定义整个ControlTemplate

<ControlTemplate x:Key="validationTest">
    <AdornedElementPlaceholder x:Name="placeholder">
        <Grid SnapsToDevicePixels="True">
            <Rectangle x:Name="PopupTargetElement"
                           Margin="-5 0 -5 0"
                           DataContext="{Binding ElementName=placeholder}"
                           StrokeThickness="0" />
            <Border x:Name="ValidationErrorElement"
                        BorderBrush="{DynamicResource ControlsValidationBrush}"
                        BorderThickness="1"
                        DataContext="{Binding ElementName=placeholder}">

                <Grid Width="12"
                          Height="12"
                          Margin="1 -4 -4 0"
                          HorizontalAlignment="Right"
                          VerticalAlignment="Top"
                          Background="Transparent">
                    <Path Margin="1 3 0 0"
                              Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z"
                              Fill="{DynamicResource ValidationBrush5}" />
                    <Path Margin="1 3 0 0"
                              Data="M 0,0 L2,0 L 8,6 L8,8"
                              Fill="{DynamicResource WhiteColorBrush}" />
                </Grid>
            </Border>

            <Controls:CustomValidationPopup x:Name="ValidationPopup"
                                                AllowsTransparency="True"
                                                IsOpen="False"
                                                PlacementTarget="{Binding ElementName=PopupTargetElement, Mode=OneWay}">
                <Grid x:Name="Root" Margin="4">
                    <Grid.Resources>
                        <Style TargetType="Border">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=Root, Path=IsMouseOver, Mode=OneWay}" Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                                                     To="0.15"
                                                                     Duration="0:0:0.1" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.EnterActions>
                                    <DataTrigger.ExitActions>
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                                                     To="1"
                                                                     Duration="0:0:0.1" />
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.ExitActions>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Grid.Resources>
                    <Border Margin="4 4 -4 -4"
                                Background="{DynamicResource ValidationBrush1}"
                                CornerRadius="5" />
                    <Border Margin="3 3 -3 -3"
                                Background="{DynamicResource ValidationBrush2}"
                                CornerRadius="4" />
                    <Border Margin="2 2 -2 -2"
                                Background="{DynamicResource ValidationBrush3}"
                                CornerRadius="3" />
                    <Border Margin="1 1 -1 -1"
                                Background="{DynamicResource ValidationBrush4}"
                                CornerRadius="2" />
                    <Border Background="{DynamicResource ValidationBrush5}" CornerRadius="2">
                        <Border.Resources>
                            <DataTemplate DataType="{x:Type ValidationError}">
                                <TextBlock MaxWidth="250"
                                               Margin="8 4 8 4"
                                               Foreground="{DynamicResource ValidationTextBrush}"
                                               Text="{Binding ErrorContent}"
                                               TextWrapping="Wrap"
                                               UseLayoutRounding="False" />
                            </DataTemplate>
                        </Border.Resources>
                        <ItemsControl ItemsSource="{Binding}" />
                    </Border>
                </Grid>
            </Controls:CustomValidationPopup>
        </Grid>
    </AdornedElementPlaceholder>
    <ControlTemplate.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.IsKeyboardFocusWithin, Mode=OneWay}" Value="True" />
                <Condition Binding="{Binding ElementName=placeholder, Path=AdornedElement.(Validation.HasError), Mode=OneWay}" Value="True" />
            </MultiDataTrigger.Conditions>
            <Setter TargetName="ValidationPopup" Property="IsOpen" Value="True" />
        </MultiDataTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>