带工具提示的WPF自定义验证器

时间:2010-06-23 22:51:53

标签: wpf validation tooltip controltemplate

我想为我的WPF应用创建一个自定义验证器模板。

我有一个工具提示模板:

    <ControlTemplate x:Key="ToolTipTemplate" TargetType="ToolTip">
        <Grid Opacity="0.93" MaxWidth="200">
            <Border BorderBrush="Black" BorderThickness="1,1,1,1" CornerRadius="2">
                <Border.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFF5FAFE" Offset="0"/>
                        <GradientStop Color="#FFE5EFF9" Offset="1"/>
                    </LinearGradientBrush>
                </Border.Background>
                <Border.Effect>
                    <DropShadowEffect Color="#FF393939" />
                </Border.Effect>
                <TextBlock Margin="10,10,10,10"
                    VerticalAlignment="Top"
                    TextWrapping="Wrap"
                    HorizontalAlignment="Left"  
                    Text="{TemplateBinding Content}">
                </TextBlock>
            </Border>
        </Grid>
    </ControlTemplate>

验证的另一个模板:

   <ControlTemplate x:Key="GlobalErrorTemplate">
        <DockPanel>
            <Ellipse  x:Name="PART_icon"
                DockPanel.Dock="Right" 
                Margin="2,0"
                Width="10" Height="10">
                <Ellipse.Fill>
                    <LinearGradientBrush>
                        <GradientStop Color="#11FF1111" Offset="0" />
                        <GradientStop Color="#FFFF0000" Offset="1" />
                    </LinearGradientBrush>
                </Ellipse.Fill>
                <Ellipse.ToolTip>
                    <ToolTip Template="{StaticResource ToolTipTemplate}" />                        
                </Ellipse.ToolTip>
            </Ellipse>
            <AdornedElementPlaceholder x:Name="adorner" />
        </DockPanel>
    </ControlTemplate>

控件的样式,例如:

    <Style TargetType="TextBox">
        <Setter Property="Validation.ErrorTemplate" Value="{StaticResource GlobalErrorTemplate}"/>
        <!--<Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
            </Trigger>
        </Style.Triggers>-->
    </Style>

如何将Ellipse的工具提示设置为(Validation.Errors)[0] .ErrorContent

谢谢, 玛特

1 个答案:

答案 0 :(得分:7)

ErrorTemplate的DataContext已经是Validation.Errors的值,因此您只需将工具提示定义为:

<ToolTip Template="{StaticResource ToolTipTemplate}" Content="{Binding Path=[0].ErrorContent}" />