WPF custom validator with tooltip

2019-04-07 22:58发布

I'd like to create a custom validator template for my WPF app.

I have a tooltip template:

    <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>

Another template for validation:

   <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>

And the styles for controls, eg:

    <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>

How can I set the tooltip of the Ellipse to (Validation.Errors)[0].ErrorContent?

Thanks, Mart

1条回答
对你真心纯属浪费
2楼-- · 2019-04-07 23:44

The DataContext of the ErrorTemplate is already the value of Validation.Errors, so you can just define your tooltip as:

<ToolTip Template="{StaticResource ToolTipTemplate}" Content="{Binding Path=[0].ErrorContent}" />
查看更多
登录 后发表回答