在自定义错误模板上显示工具提示

时间:2014-06-23 07:16:55

标签: c# wpf

我创建了一个自定义错误模板来显示验证错误。这是我的XAML:

<Style TargetType="Control" x:Key="myErrorTemplate">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="26"
                               FontWeight="Bold"
                               Text=" !"
                               Margin="0,-8,0,0" />
                    <Border>
                        <AdornedElementPlaceholder Name="myControl" />
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
            Path=(Validation.Errors)[0].ErrorContent}" />
        </Trigger>
    </Style.Triggers>
</Style>

当发生验证错误时,这将在TextBox附近放置一个感叹号。当鼠标悬停在TextBox上时,此模板将显示错误工具提示。当我将鼠标悬停在感叹号(文本块)上时,我还想显示工具提示。我如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

试试这个:

       <TextBlock DockPanel.Dock="Right"
                           Foreground="Red"
                           FontSize="26"
                           FontWeight="Bold"
                           Text=" !"
                           Margin="0,-8,0,0" >
            <TextBlock.Style>
                  <Style TargetType="TextBlock">
                      <Style.Triggers>
                         <DataTrigger Binding="{Binding Path=(Validation.HasError), RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                           <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource TemplatedParent},
        Path=(Validation.Errors)[0].ErrorContent}" />
                       </DataTrigger>
                      </Style.Triggers> 
                  </Style>
            </TextBlock.Style>
       </TextBlock>