lusian_andrei
lusian_andrei

Reputation: 117

Validation.HasError tooltip content

I'm trying to create a custom validation rules mechanism. I want to switch the tooltip style and content when the Validation.HasError is true. For this I'm using this:

<Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
        <Setter Property="ToolTip">
            <Setter.Value>
                <ToolTip
                    ToolTip="{Binding  Path=(Validation.Errors)[0].ErrorContent}"
                    Tag="{Binding  Path=(Validation.Errors)[0].ErrorContent, Converter={StaticResource TooltipJoin}}"
                    Style="{StaticResource WarningTooltip}"/>
            </Setter.Value>
        </Setter>
    </Trigger>
</Style.Triggers>

The warning Tooltip template is:

<Border BorderThickness="1"
    BorderBrush="{DynamicResource FeedackBrush}"
    Background="{DynamicResource Color}"
    MinWidth="42" CornerRadius="2"
    Padding="5,3,5,3">

    <!--Holds the image and text for the tooltip-->
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <ContentPresenter
          Grid.Column="0"
          Content="{DynamicResource WarningIcon}"
          Width="32"
          Height="32"/>

        <!--Display the text-->
        <TextBox Background="Transparent"
             BorderThickness="0"
             VerticalAlignment="Center"
             Grid.Column="1"
             Foreground="{DynamicResource ForgroundColor}"
             Text="{TemplateBinding Content}"
             TextAlignment="Center"
             BorderBrush="Transparent"/>
    </Grid>
</Border>

The validation method:

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  {
     try
     {
        if (value != null)
        {
           var index = (int)value;
           if (index == 2 || index == 5)
           {
              return new ValidationResult(true, null);
           }
        }
     }
     catch (Exception e)
     {

        return new ValidationResult(false, e.Message);
     }

     return new ValidationResult(false, "Please select corect item");
  }

And in xaml :

<ComboBox Name="MyCombo"
            Grid.Row="0" 
            Grid.Column="0" 
            BorderThickness="5"
            ItemsSource="{Binding MyItemsCollection,UpdateSourceTrigger=PropertyChanged}"
            Height="30" 
           Margin="50,178,50,42">
        <ComboBox.SelectedIndex>
            <Binding Path="Index" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules >
                    <c:ComboBoxRule ItemIdex="2" ValidatesOnTargetUpdated="True" />
                </Binding.ValidationRules>
            </Binding>
        </ComboBox.SelectedIndex>
</ComboBox>

I can switch the style between the general style and the warning tool tip style but the content is empty. Any suggestions for the content problem?

Upvotes: 0

Views: 795

Answers (2)

King King
King King

Reputation: 63327

I believe this is your problem. The ToolTip should have Content set, not its ToolTip. Also you need to specify some RelativeSource with BindingMode of Self to target the styled element instead of using the implicit DataContext (which comes from your viewmodel):

<Setter.Value>
      <ToolTip
           Content="{Binding Path=(Validation.Errors)[0].ErrorContent,
                             RelativeSource={RelativeSource Self}}"
           Tag="{Binding Path=(Validation.Errors)[0].ErrorContent, 
                 Converter={StaticResource TooltipJoin},
                 RelativeSource={RelativeSource Self}}"
           Style="{StaticResource WarningTooltip}"/>
</Setter.Value>

Upvotes: 1

lusian_andrei
lusian_andrei

Reputation: 117

Find somehow a workaround solution : I put this in trigger

 <Trigger Property="Validation.HasError" Value="true">
             <Setter Property="ToolTip">
                    <Setter.Value>
                        <ToolTip  Style="{StaticResource WarningTooltip}"/>
                    </Setter.Value>
             </Setter>
                <Setter Property="Tag" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent, Converter={StaticResource TooltipJoin}}"/>
            </Trigger>

and for the textbox content i'm using something like this :

Content="{Binding Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Control}, AncestorLevel=2}}"

Upvotes: 0

Related Questions