没有验证错误WPF时不显示工具提示 [英] Tooltip Not Showing Up When No Validation Error WPF

查看:91
本文介绍了没有验证错误WPF时不显示工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了,没有找到解决方法.

I searched and did not see a solution.

仅当我未在组合框的tooltip属性中设置工具提示时,我才能获得验证以显示工具提示.我希望看到存在验证错误的工具提示,否则请从combobox属性中显示工具提示.当我从工具提示属性(即从组合框的属性面板)删除文本时,验证工具提示显示得很好.

I can only get the validation to show the tooltip if I do not set a tooltip in the combo box tooltip property. I would like to see the validation error tooltip when one is present otherwise show the tooltip from the combobox property. The validation tooltip shows up fine when I remove the text from the tooltip property (i.e. from the property panel for the combo box).

用于提示验证错误的工具提示的Application.Resources(App.XAML)中的XAML是

The XAML in Application.Resources (App.XAML)for the tooltip to show the validation error is

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

我还为组合框使用验证模板,如下所示.这位于用户控件cs文件的UserControl.Resources部分中.

I also use a validation template for the Combobox as follows. This is in the UserControl.Resources section within the user control cs file.

<ControlTemplate x:Key="comboBoxValidationTemplate">
    <DockPanel Name="myDockPanel">
        <Border BorderBrush="Red" BorderThickness="3">
            <AdornedElementPlaceholder Name="MyAdorner" />
        </Border>
        <TextBlock Text="*" FontWeight="Bold" FontSize="18" Foreground="Red" DockPanel.Dock="Left" />
    </DockPanel>
</ControlTemplate>

控件本身定义如下.请注意,这里还没有定义其他参考(但希望没有相关意义-如有疑问,请随时告诉我).

The control itself is defined as follows. Note that there are other references not defined here (but hopefully not pertinent - feel free to let me know if questions).

        <ComboBox x:Name="ExposureTime" SelectedValuePath="Content"
        Text="{Binding ExposureTime, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" IsEditable="True" Validation.ErrorTemplate="{StaticResource comboBoxValidationTemplate}"
        HorizontalContentAlignment="Right" FontSize="18" Margin="136,47,462,0" Height="27" VerticalAlignment="Top" GotFocus="ComboBox_GotFocus_1" LostFocus="ComboBox_LostFocus_1" PreviewTextInput="ExposureTime_PreviewTextInput" Opacity="{Binding BackgroundOpacity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontWeight="Thin" Style="{DynamicResource StandardComboBoxStyle}" SelectedValue="{Binding Mode=OneWay, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" IsTextSearchEnabled="False" ToolTip="My tooltip test.">
        <ComboBoxItem Content="0.05"/>
        <ComboBoxItem Content="0.1"/>
        <ComboBoxItem Content="0.2" />
        <ComboBoxItem Content="1" />
        <ComboBoxItem Content="2" />
        <ComboBoxItem Content="5" />
        <ComboBoxItem Content="10" />
        <ComboBoxItem Content="20" />
        <ComboBoxItem Content="60" />
        <ComboBox.IsEnabled >
            <MultiBinding Converter="{StaticResource multiBooleanConverter}">
                <Binding Path="NotPerformingExposure" UpdateSourceTrigger="PropertyChanged"/>Th
                <Binding Path="NotPerformingFocusTest" UpdateSourceTrigger="PropertyChanged"/>
            </MultiBinding>
        </ComboBox.IsEnabled>
    </ComboBox>

谢谢! 巴克

推荐答案

在发生样式错误时,您将在样式触发器中将工具提示设置为Validation错误.当您没有错误时,可以通过操作Trigger

In your style triggers you set the tooltip to the Validation error when you have an error. You can do the same when you don't have an error by manipulating the Value property of the Trigger

<Style x:Key="StandardComboBoxStyle" TargetType="{x:Type ComboBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
        <Trigger Property="Validation.HasError" Value="False">
            <Setter Property="ToolTip" Value="My tooltip test." />
        </Trigger>
    </Style.Triggers>
</Style>

另一方面,我建议将Path=(Validation.Errors)[0].ErrorContent更改为Path=(Validation.Errors).CurrentItem.ErrorContent

On another note I'd recommend changing Path=(Validation.Errors)[0].ErrorContent to Path=(Validation.Errors).CurrentItem.ErrorContent

这篇关于没有验证错误WPF时不显示工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆