【问题标题】:Show tooltip on custom error template在自定义错误模板上显示工具提示
【发布时间】:2014-06-23 17:47:04
【问题描述】:

我创建了一个自定义错误模板来显示验证错误。这是我的 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 附近放置一个感叹号。目前,当鼠标悬停在文本框上时,此模板将显示错误工具提示。当我将鼠标悬停在感叹号(文本块)上时,我也想显示工具提示。我如何做到这一点?

【问题讨论】:

    标签: c# wpf


    【解决方案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>
    

    【讨论】:

    • ,它不起作用我收到绑定错误,BindingExpression 路径错误:在“对象”“控件”(名称=“”)上找不到“验证”属性。 BindingExpression:Path=Validation.HasError;数据项='控制'(名称='');目标元素是'TextBlock'(名称='');目标属性是“NoTarget”(类型“对象”)
    • ,现在我收到另一个错误 XamlParse 异常,它说:{“TwoWay 或 OneWayToSource 绑定无法对 'System.Windows.Controls.Control' 类型的只读属性 'HasError' 起作用。” }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多