【问题标题】:Material Design and IntegerUpDown Tooltip to Display Error显示错误的材料设计和 IntegerUpDown 工具提示
【发布时间】:2018-02-26 17:28:08
【问题描述】:

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

通过使用以下全局样式,我已将其与 TextBoxes 和 TextBlocks 一起使用:

<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 调试器中运行并触发错误条件时,输出窗口中不会显示有关失败绑定或其他内容的错误消息。

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

【问题讨论】:

    标签: c# wpf xaml wpftoolkit xceed


    【解决方案1】:

    您没有显示您是如何绑定它的,但请记住 1) 指定 UpdateSourceTrigger 和 2) 实际将鼠标悬停在它上面!这是一个MCVE,表明它实际上没有问题。

    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>
    

    【讨论】:

    • 谢谢,@jsanalytics。除了回答我的问题之外,您还指出了为什么单击 IntegerUpDown 时我的视图模型没有更新。快速跟进问题:你知道为什么 IntegerUpDown 控件的 UpdateSourceTrigger 不默认为 PropertyChanged 吗?似乎应该 - 如果我更改控件的值,为什么我不想立即知道这一点,而不是何时失去焦点?
    • 我倾向于同意你的观点。我不明白为什么不...话虽如此,请记住,大多数 WPF 内置控件都是相当复杂的对象,彼此之间存在着令人生畏的“依赖关系”网络,因此任何人都不会感到惊讶,以防万一实际上有一个(或多个)原因导致默认不是显而易见的选项。这么多废话废话说“我真的不知道”......:)
    • 我怀疑你可能是对的。有趣的是,我最初解决了我的“为什么视图模型没有更新?”通过编写我自己的 MvvM Light RelayCommand 来解决问题,我将其配置为每次 IntegerUpDown 控件引发其 ValueChanged 事件时跳闸。谈论在谷仓周围一路走来...... :)
    猜你喜欢
    • 1970-01-01
    • 2019-05-03
    • 2019-11-21
    • 2016-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2021-11-07
    • 1970-01-01
    相关资源
    最近更新 更多