AdornedElementPlaceholder无效:更改选项卡时验证边框消失

时间:2015-07-21 04:38:49

标签: wpf

我的应用程序中有各种文本框,我使用红色边框来验证输入的数据。问题是,当我更改标签时,红色边框消失。所以我试图在我的样式文件中应用AdornedElementPlaceholder,以便所有文本框都能适应这种行为。我已经尝试了下面提到的代码,但粗体代码行(AdornedElementPlaceholder)不起作用。我正在使用WPF和C#。有人可以帮忙吗?

    <Style x:Key="TextBoxStyle"  TargetType="{x:Type TextBox}" >
                <Setter Property="Width" Value="120" />
                <Setter Property="Height" Value="25" />   
                <Setter Property="FontFamily" Value="Verdana"/>
                <Setter Property="FontSize" Value="12"/>
                <Setter Property="VerticalAlignment" Value="Top"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TextBoxBase}">
                            <Border x:Name="bg" 
                        CornerRadius="5"
                        Padding="2"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{StaticResource NormalBorderBrush}"
                        BorderThickness="1" >
                       **<AdornedElementPlaceholder/>**
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="BorderBrush"                           
                                      TargetName="bg" Value="#82CAFA"/>
                                </Trigger>
                                <Trigger Property="IsFocused" Value="True">
                                    <Setter Property="BorderBrush" 
                                     TargetName="bg" Value="#1589FF"/>
                                </Trigger>
                                <Trigger Property="IsReadOnly" Value="True">
                                    <Setter Property="Background"                    
                                      Value="#E5E4E2"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="SpellCheck.IsEnabled" Value="True" />
                <Setter Property="Language"  Value="en-gb" />
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="BorderBrush" Value="{StaticResource 
                          ErrorBackground}" />
                        <Setter Property="ToolTip" Value="{Binding 
                        RelativeSource={RelativeSource Self}, Path=
                      (Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>        
            </Style>

1 个答案:

答案 0 :(得分:0)

<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}"/>
    <Setter Property="Width" Value="120"/>
    <Setter Property="Height" Value="25"/>
    <Setter Property="FontFamily" Value="Verdana"/>
    <Setter Property="FontSize" Value="12"/>
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="SpellCheck.IsEnabled" Value="True"/>
    <Setter Property="Language" Value="en-gb"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border CornerRadius="5" Padding="2" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
                    <ScrollViewer x:Name="PART_ContentHost" Padding="{TemplateBinding Padding}" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="#82CAFA"/>
        </Trigger>
        <Trigger Property="IsFocused" Value="True">
            <Setter Property="BorderBrush" Value="#1589FF"/>
        </Trigger>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="Background" Value="#E5E4E2"/>
        </Trigger>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="BorderBrush" Value="{StaticResource ErrorBackground}"/>
            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>