工具提示未正确显示

时间:2013-03-27 16:44:37

标签: c# wpf xaml textbox tooltip

我是WPF的新手。我希望在TextBox发生错误时显示自定义工具提示。所以我使用了Style,如下所示:

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">                   
                <Setter Property="ToolTip">
                    <Setter.Value>
                        <ToolTip Placement="Top">                            
                            <Border Margin="-4,0,-4,-3" Padding="2" Background="Red">
                                <Border.BitmapEffect>
                                    <OuterGlowBitmapEffect></OuterGlowBitmapEffect>
                                </Border.BitmapEffect>
                                <Label Foreground="White" FontWeight="Bold" Content="{Binding RelativeSource={RelativeSource AncestorType={x:Type TextBox}}, Path=(Validation.Errors)[0].ErrorContent}"></Label>
                            </Border>
                        </ToolTip>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

以上代码未在ToolTip中显示错误消息。你能帮忙解决这个问题吗?任何帮助都会非常值得赞赏。

2 个答案:

答案 0 :(得分:2)

您应该使用以下样式(msdn)更改当前样式:

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <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>

您应该为ToolTip创建样式:

<Style TargetType="ToolTip">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="HasDropShadow" Value="True"/>
    <Setter Property="Placement" Value="Top" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border Name="Border" Margin="-4,0,-4,-3" Padding="2" Background="Red">
                    <Border.BitmapEffect>
                        <OuterGlowBitmapEffect></OuterGlowBitmapEffect>
                    </Border.BitmapEffect>
                    <ContentPresenter
                        Margin="4" 
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top" 
                        TextElement.Foreground="White" TextElement.FontWeight="Bold"                                      
                        />                                    
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasDropShadow" Value="true">
                        <Setter TargetName="Border" Property="CornerRadius" Value="4"/>
                        <Setter TargetName="Border" Property="SnapsToDevicePixels" Value="true"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

使用示例:

<TextBox Text="{Binding MyText, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" 
         Style="{StaticResource textBoxInError}"                     
         />

答案 1 :(得分:1)

您还应该替换

Path=(Validation.Errors)[0].ErrorContent

Path=(Validation.Errors).CurrentItem.ErrorContent}

因为在您的情况下,当没有验证错误时,您将获得静默的ArgumentOutOfRangeException。不要忘记实现IDataErrorInfo或INotifyDataErrorInfo。