C# 用于显示错误的材质设计和集成下拉工具提示

C# 用于显示错误的材质设计和集成下拉工具提示,c#,wpf,xaml,wpftoolkit,xceed,C#,Wpf,Xaml,Wpftoolkit,Xceed,我正在WPF项目中使用MaterialDesign和Xceed IntegerUpDown控件。当鼠标悬停在控件上时,我试图将与updown控件关联的错误显示为工具提示 通过使用以下全局样式,我已将其用于文本框和文本块: <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource CustomizedMaterialDesignTextBox}" > <Setter Property="Ma

我正在WPF项目中使用MaterialDesign和Xceed IntegerUpDown控件。当鼠标悬停在控件上时,我试图将与updown控件关联的错误显示为工具提示

通过使用以下全局样式,我已将其用于文本框和文本块:

<Style TargetType="{x:Type TextBox}" 
       BasedOn="{StaticResource CustomizedMaterialDesignTextBox}" >
    <Setter Property="Margin" Value="5"/>
    <Setter Property="VerticalAlignment" Value="Center"/>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}"
                             Background="{DynamicResource ValidationErrorBrush}">
                        <ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}"
                                      DisplayMemberPath="ErrorContent"/>
                    </ToolTip>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

但是,当我尝试按如下方式将其调整为updown控件时,不会显示任何工具提示:

<Style TargetType="{x:Type xctk:IntegerUpDown}">
    <Setter Property="Margin" Value="5"/>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}"
                             Background="{DynamicResource ValidationErrorBrush}">
                        <ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}"
                                      DisplayMemberPath="ErrorContent"/>
                    </ToolTip>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

在VS 2017调试器中运行应用程序并触发错误条件时,输出窗口中不会显示有关绑定失败或诸如此类的错误消息


有趣的是,当存在错误条件时,控件周围会出现一个红色边框,即使没有自定义的上下样式。

您没有显示绑定它的确切方式,但请记住1)指定
更新资源触发器
和2)实际将鼠标悬停在它上面!这是一个显示,它实际上工作没有问题

XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
    xmlns:local="clr-namespace:WpfApp5"
    x:Class="WpfApp5.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>

    <Style TargetType="{x:Type xctk:IntegerUpDown}">
        <Setter Property="Margin" Value="5"/>

        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="ToolTip">
                    <Setter.Value>
                        <ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}"
                         Background="{DynamicResource ValidationErrorBrush}">
                            <ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}"
                                  DisplayMemberPath="ErrorContent"/>
                        </ToolTip>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>

</Window.Resources>

<Window.DataContext>
    <local:SampleViewModel/>
</Window.DataContext>

<Grid>
    <xctk:IntegerUpDown Text="{Binding MyInteger, UpdateSourceTrigger=PropertyChanged}"
                        FontSize="24"
                        Width="125"
                        Height="50"/>
</Grid>


Thanx,@jsanalytics。除了回答我的问题外,您还指出了单击IntegerUpDown时为什么没有更新我的viewmodel。快速跟进问题:您知道为什么UpdateSourceTrigger不会默认为IntegerUpDown控件的PropertyChanged吗?它似乎应该--如果我改变控件的值,为什么我不想马上知道,而不是当焦点丢失时?我倾向于同意你。我不明白为什么不。。。话虽如此,请记住,大多数WPF内置控件都是相当复杂的对象,彼此之间存在着令人生畏的“依赖性”网络,因此,如果有一个(或多个)原因表明默认控件不是显而易见的选项,那么任何人都不会感到惊讶。说“我真的不知道”…)我想你可能是对的。有趣的是,我最初通过编写自己的MvvM Light RelayCommand解决了我的“为什么viewmodel没有更新?”问题,我将该命令配置为每次IntegerDown控件引发其ValueChanged事件时都会触发。谈论绕着谷仓走一圈……:)