如何在Wpf中

时间:2016-04-26 20:52:37

标签: wpf validation templates styles

我处理的文本框样式显示红色边框,错误图像和新行上的错误文本。 如果文本框底部有空格,如下图所示,它可以工作。 Style1

如果在验证文本框下方有其他控件,则我看不到图标和错误文本。

Style2
修复此模板的最佳方法是什么?

<Setter Property="Template">
   <Setter.Value>
       <ControlTemplate TargetType="{x:Type TextBox}">
                    <StackPanel>
                        <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                            <ScrollViewer VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <Grid x:Name="errorBlock" Visibility="Hidden" Margin="0,2,0,-30" Z>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Image Grid.Column="0" Source="Error.png"  Height="14" Width="14"
                                   HorizontalAlignment="Left"  VerticalAlignment="Center"  Margin="0,0,5,0"/>
                            <TextBlock Grid.Column="1" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TextBox}},Path=(Validation.Errors)[0].ErrorContent}"  />
                        </Grid>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="Validation.HasError" Value="True"/>
                                <Condition Property="IsEnabled" Value="True"/>
                            </MultiTrigger.Conditions>
                            <Setter Property="Visibility"  TargetName="errorBlock" Value="Visible" />                           
                        </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

1 个答案:

答案 0 :(得分:0)

我认为你不应该在保证金上使用-30 - 它会产生一些意想不到的行为。我宁愿使用两行Grid而不是StackPanel,并将第二行高度设置为Auto,Visibility =&#34; Collapsed&#34;。

    <ControlTemplate TargetType="{x:Type TextBox}">
        <Grid>
            <Grid.RowDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.RowDefinitions>
            <Border x:Name="Bd" >
                <ScrollViewer x:Name="PART_ContentHost" />
            </Border>
            <Grid x:Name="errorBlock" Visibility="Collapsed" Height="14" Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="19"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="Error.png" Height="14" Width="14" Margin="0,0,5,0"/>
                <TextBlock Grid.Column="1" Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TextBox}},Path=(Validation.Errors)[0].ErrorContent}"  />
            </Grid>
        </Grid>
        <ControlTemplate.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Validation.HasError" Value="True"/>
                    <Condition Property="IsEnabled" Value="True"/>
                </MultiTrigger.Conditions>
                <Setter Property="Visibility"  TargetName="errorBlock" Value="Visible" />
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>