c# - WPF 工具提示触发器不工作

标签 c# wpf mvvm tooltip

我正在实现 在我的 ViewModel 中对我的一些绑定(bind)属性执行验证。然后我尝试使用以下设置 并自动显示:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
                <Setter Property="ToolTip.IsOpen" Value="True"></Setter>
                <Setter Property="ToolTip.StaysOpen" Value="True"></Setter>
                <Setter Property="ToolTip.Placement" Value="Bottom"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style> 

问题是虽然 正在设置它不会出现,除非我将光标放在输入上。我想在验证失败时自动显示工具提示。

我可以想出很多方法来验证数据,但我正在寻找一种简化、易于重用并且不会弄乱我的其余代码(和 UI)的解决方案。如果有更好的方法这样做,然后我想听听。

干杯

最佳答案

我同意 Viv 的观点。我使用样式为样式中的文本框定义 validation.errortemplate。它在带有错误消息的控件下添加了一个红色框。你可以试试这个并调整它。我不认为强制工具提示是正确的想法。如果您确实必须具有该功能,我会使用 validation.errortemplate 中的弹出窗口。

<Style x:Key="FTC_ValidateTextBox" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Open Sans Condensed"/>
    <Setter Property="FontSize" Value="19" />
    <Setter Property="Margin" Value="3,3,15,6"/>
    <Setter Property="Padding" Value="10,3"/>
    <Setter Property="TextWrapping" Value="Wrap" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Background" Value="{StaticResource DetailTextBox}" />
    <Setter Property="BorderBrush" Value="{StaticResource MediumGray}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <Border BorderBrush="{StaticResource MediumRed}" >
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <AdornedElementPlaceholder x:Name="parentTextBox" />
                        <TextBlock Grid.Row="1" Style="{StaticResource DetailError}"
                            Text="{Binding AdornedElement.(Validation.Errors).CurrentItem.ErrorContent, ElementName=parentTextBox}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding (Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
            <Setter Property="BorderBrush" Value="{StaticResource MediumRed}"/>
            <Setter Property="Foreground" Value="{StaticResource MediumRed}"/>
            <Setter Property="Margin" Value="3,3,15,31"/>
        </Trigger>
    </Style.Triggers>
</Style>

关于c# - WPF 工具提示触发器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15972668/

相关文章:

c# - SelectedItem 和 SelectedValue 返回错误的项目

c# - 在XAML中将CollectionViewSource与CompositeCollection结合使用

c# - 在 C# 中的枚举值之间迭代

c# - 如何在控制台中将我的项目对齐在同一行?

javascript - 在 Angularjs 中通过 IP 地址获取用户的位置

c# - 在 WPF 的自定义 main() 中获取对 MainWindow 的引用

wpf - MVVM样式的TreeView元素上的ContextMenu

wpf - .ico 中的图像作为 DynamicResource

wpf - 用于关闭 View 的 ViewModel 命令的 Cinch 版本

c# - 从应用程序的多个线程到数据库的多个连接是否会提高插入查询性能?