WPF:ValidationError时TextBox附加的红框

时间:2016-02-03 00:48:40

标签: c# wpf

当我得到ValidationError时,我无法弄清楚为什么我的TextBox周围会出现额外的红色边框!我只想在我的边框周围设置风格的边框颜色,但是周围有一个额外的矩形边框,我无法弄清楚如何摆脱它!

见图片: enter image description here

这是我的TextBox样式:

 <Style x:Key="DefaultTextbox" TargetType="TextBox" xmlns:Helpers="clr-namespace:PhotoManagement.Helpers">
        <Setter Property="Helpers:WaterMarkTextHelper.IsMonitoring" Value="True"/>
        <Setter Property="Helpers:WaterMarkTextHelper.WatermarkText" Value="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" />
        <Setter Property="Background" Value="#FF22252C" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="BorderBrush" Value="#FF22252C" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Cursor" Value="IBeam" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="TextBox">
                    <DockPanel>
                        <Popup Name="ErrorPopup" IsOpen="False" Placement="Top">
                            <TextBlock Background="White" Foreground="Black" Text="Test" Padding="5" />
                        </Popup>
                        <Border CornerRadius="5" 
                                    BorderBrush="{TemplateBinding BorderBrush}"
                                    BorderThickness="1"
                                    Background="{TemplateBinding Background}" 
                                    Margin="3" 
                                    MinWidth="{TemplateBinding MinWidth}">
                            <Grid>
                                <StackPanel Margin="8">
                                    <ScrollViewer x:Name="PART_ContentHost"/>
                                </StackPanel>
                                <TextBlock Name="PART_TempText" 
                                               Text="{TemplateBinding Tag}"
                                               Foreground="#FF454954"
                                               Padding="8"
                                               Margin="5 0" />
                            </Grid>
                        </Border>
                    </DockPanel>
                    <ControlTemplate.Triggers>
                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="BorderBrush" Value="Red" />
                           <Setter TargetName="ErrorPopup" Property="IsOpen" Value="True" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Helpers:WaterMarkTextHelper.HasText" Value="False"/>
                                <Condition Property="IsFocused" Value="True"/>
                            </MultiTrigger.Conditions>
                            <MultiTrigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource enterGotFocus}"/>
                            </MultiTrigger.EnterActions>
                            <MultiTrigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource exitGotFocus}"/>
                            </MultiTrigger.ExitActions>
                        </MultiTrigger>

                        <Trigger Property="Helpers:WaterMarkTextHelper.HasText" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard Storyboard="{StaticResource enterHasText}"/>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard Storyboard="{StaticResource exitHasText}"/>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这是我在ContentControl上的ControlTemplate,我发布在这篇文章的底部,其中包含TextBox的。

<ControlTemplate x:Key="InputFields" TargetType="ContentControl">
        <Grid>
            <i:Interaction.Triggers>
                <local:RoutedEventTrigger RoutedEvent="{x:Static Validation.ErrorEvent}">
                    <e2c:EventToCommand
                        Command="{Binding EditVM.TheEntity.ConversionErrorCommand, Mode=OneWay}"
                        EventArgsConverter="{StaticResource BindingErrorEventArgsConverter}"
                        PassEventArgsToCommand="True" />
                </local:RoutedEventTrigger>
                <local:RoutedEventTrigger RoutedEvent="{x:Static Binding.SourceUpdatedEvent}">
                    <e2c:EventToCommand
                        Command="{Binding EditVM.TheEntity.SourceUpdatedCommand, Mode=OneWay}"
                        EventArgsConverter="{StaticResource BindingSourcePropertyConverter}"
                        PassEventArgsToCommand="True" />
                </local:RoutedEventTrigger>
            </i:Interaction.Triggers>
            <ContentPresenter />
        </Grid>
    </ControlTemplate>
 <Style x:Key="ErrorToolTip" TargetType="Control">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, 
                                            Path=(Validation.Errors), Converter={StaticResource ErrorCollectionConverter}}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

ContentControl中

<ContentControl Template="{StaticResource InputFields}">
                    <StackPanel>
                        <WrapPanel>
                            <TextBox Tag="First Name..." 
                                         Text="{Binding EditVM.TheEntity.FName,
                                    UpdateSourceTrigger=PropertyChanged,
                                    NotifyOnSourceUpdated=True,
                                    NotifyOnValidationError=True,
                                    Mode=TwoWay}">
                                <TextBox.Resources>
                                    <Style TargetType="ToolTip">
                                        <Setter Property="StaysOpen" Value="True" />
                                        <Setter Property="Placement" Value="Center" />
                                        <Setter Property="IsOpen" Value="True" />
                                    </Style>
                                </TextBox.Resources>
                            </TextBox>

                            <TextBox Tag="Last Name..."
                                         Text="{Binding EditVM.TheEntity.LName,
                                    UpdateSourceTrigger=PropertyChanged,
                                    NotifyOnSourceUpdated=True,
                                    NotifyOnValidationError=True,
                                    Mode=TwoWay}"/>
                            <TextBox Tag="Email Address..."
                                         Text="{Binding EditVM.TheEntity.Email,
                                    UpdateSourceTrigger=PropertyChanged,
                                    NotifyOnSourceUpdated=True,
                                    NotifyOnValidationError=True,
                                    Mode=TwoWay}"/>
                        </WrapPanel>

                    </StackPanel>
                </ContentControl>

1 个答案:

答案 0 :(得分:0)

不太确定这是否是最佳答案,但我设法通过在TextBox样式中添加以下内容来修复它:

<Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <AdornedElementPlaceholder />
                </ControlTemplate>
            </Setter.Value>
        </Setter>