【问题标题】:How to make custom template for validation rule's default tooltip?如何为验证规则的默认工具提示制作自定义模板?
【发布时间】:2020-12-06 08:23:06
【问题描述】:

在使用验证规则时,如何为自动应用于控件的默认工具提示制作自定义模板?还有有没有办法改变这个工具提示的持续时间?

我的意思是这个工具提示:

XAML:

<Window.Resources>
   <!--Validation Template-->
   <ControlTemplate x:Key="MyErrorTemplate">
      <Border BorderBrush="#e92539" BorderThickness="2" CornerRadius="10">
         <Grid>
            <AdornedElementPlaceholder/>
         </Grid>
      </Border>
   </ControlTemplate>
</Window.Resources>
<TextBox FontSize="13" Validation.ErrorTemplate="{StaticResource MyErrorTemplate}"
         AcceptsReturn="False" Panel.ZIndex="3" x:Name="txtName"
         Style="{StaticResource MyTextBox}" Grid.Row="2">
   <TextBox.Text>
      <Binding Path="txtName" Mode="TwoWay"
               UpdateSourceTrigger="PropertyChanged"
               ValidatesOnDataErrors="True">
         <Binding.ValidationRules>
            <local:ValidateTextBox/>
         </Binding.ValidationRules>
      </Binding>
   </TextBox.Text>
</TextBox>

C#:

class ValidateTextBox : ValidationRule
{
    Regex regex = new Regex(@"[\\/:*?""<>|]");

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        string txtNameText = value as string;
        if (regex.IsMatch(txtNameText))
        {
            return new ValidationResult(false, "A file name can't contain any of the following characters:\n \\ / : * ? \" < > |");
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }
}

【问题讨论】:

    标签: c# wpf validation xaml controltemplate


    【解决方案1】:

    您必须调整您的 TextBox 样式或创建一个新样式,将 ToolTip 设置为当前错误。

    • 第一个 setter 设置您的自定义 ToolTip 模板。
    • 第二个设置器使用ToolTipService 设置附加属性ShowDuration,该属性确定工具提示应显示多长时间。还有其他有用的属性可供探索。
    • 使用附加属性Validation.HasErrorTrigger 仅在出现错误时应用工具提示。这允许您在不同的 setter 中指定默认工具提示。
    • 由于ToolTip 与其关联控件不属于同一可视化树,因此您必须使用绑定到其PlacementTarget(附加了验证错误的TextBox)
    <Style x:Key="MyTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
       <Setter Property="Validation.ErrorTemplate" Value="{StaticResource MyErrorTemplate}"/>
       <Setter Property="ToolTipService.ShowDuration" Value="5000"/>
       <Style.Triggers>
          <Trigger Property="Validation.HasError" Value="True">
             <Setter Property="ToolTip">
                <Setter.Value>
                   <ToolTip Template="{StaticResource ToolTipTemplate}"
                            Content="{Binding PlacementTarget.(Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource Self}}"/>
                </Setter.Value>
             </Setter>
          </Trigger>
       </Style.Triggers>
    </Style>
    

    您必须调整工具提示控件模板以将TextBlockText 绑定到模板化父级(ToolTip)的Content 属性。这可以通过TemplateBinding 完成。

    <ControlTemplate x:Key="ToolTipTemplate" TargetType="ToolTip">
       <Grid MaxWidth="400">
          <Border Margin="0,0,35,35"
                  Background="#212529"
                  BorderBrush="#6C757D"
                  BorderThickness="1.7"
                  CornerRadius="5">
             <Border.Effect>
                <DropShadowEffect Color="Black"
                                  Direction="310"
                                  ShadowDepth="14"
                                  BlurRadius="34"
                                  Opacity="0.3"/>
             </Border.Effect>
             <TextBlock Margin="7"
                        Text="{TemplateBinding Content}"
                        VerticalAlignment="Top"
                        TextWrapping="Wrap"
                        HorizontalAlignment="Left"
                        Foreground="#ADB5BD">
             </TextBlock>
          </Border>
       </Grid>
    </ControlTemplate>
    

    TextBox 中删除Validation.ErrorTemplate,因为它已经包含在样式中。

    【讨论】:

    • 感谢您的回答,但现在当我将鼠标悬停在文本框上时,工具提示内容为空
    • 而且当文本框为空并且没有任何错误时,它仍然会在鼠标悬停时显示工具提示
    • @SMH 它按预期对我有用。尝试在一个空的解决方案或不应用任何其他样式或修改的情况下对此进行测试。可能有其他样式或属性设置导致问题。
    • 是的,它现在工作正常,对不起,这是我的错,我忘了删除我添加到文本框本身的工具提示。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2016-11-14
    • 2017-07-25
    相关资源
    最近更新 更多