Tooltip Not Showing Up When No Validation Error WPF
Asked Answered
G

1

5

I searched and did not see a solution.

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).

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>

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>

Thanks! Buck

Geomorphology answered 19/8, 2013 at 23:51 Comment(0)
U
9

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>

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

Uppish answered 20/8, 2013 at 0:15 Comment(1)
Great solution, but what would I do if I want to apply this to different combo boxes, each of which has a different tooltip. I.e. Combobox1 has tooltip "I am one", and ComboBox2 has tooltip "I am #2". I'm guessing something like <Setter Property="ToolTip" Value="{Tooltip}" /> but can't get it working!Russianize

© 2022 - 2024 — McMap. All rights reserved.