加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 22:05:19 所属栏目:百科 来源:网络整理
导读:我创建了一个自定义错误模板来显示验证错误.这是我的XAML: Style TargetType="Control" x:Key="myErrorTemplate" Setter Property="Validation.ErrorTemplate" Setter.Value ControlTemplate DockPanel LastChildFill="True" TextBlock DockPanel.Dock="Rig
我创建了一个自定义错误模板来显示验证错误.这是我的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" />
                    <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上时,此模板将显示错误工具提示.当我将鼠标悬停在感叹号(文本块)上时,我还想显示工具提示.我该如何实现这一目标?

解决方法

试试这个:

<TextBlock DockPanel.Dock="Right"
                           Foreground="Red"
                           FontSize="26"
                           FontWeight="Bold"
                           Text=" !"
                           Margin="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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读