WPF IDataErrorInfo issues

569 views Asked by At

I've used WPF and IDataErrorInfo in the past apps to display errors to the user via a controltemplate by putting an image in the adorner and adding a tooltip to the image like this;

<Style x:Key="textStyle" TargetType="TextBox">
  <Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <DockPanel LastChildFill="True">
          <Border BorderBrush="Orange"
                  BorderThickness="2"
                  CornerRadius="4"
                  SnapsToDevicePixels="True">
            <Border.Effect>
              <DropShadowEffect BlurRadius="10"
                                ShadowDepth="0"
                                Color="Orange" />
            </Border.Effect>
            <DockPanel>
              <Image Width="16"
                     Height="16"
                     Margin="-20,0,0,0"
                     HorizontalAlignment="Left"
                     VerticalAlignment="Center"
                     RenderOptions.BitmapScalingMode="HighQuality"
                     Source="{StaticResource imgError}"
                     ToolTip="{Binding ElementName=adornedElement,
                                       Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent}"
                     ToolTipService.ShowDuration="30000" />
              <AdornedElementPlaceholder Name="adornedElement" />
            </DockPanel>
          </Border>
        </DockPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

With the appropriate implementation of IDataErrorInfo in the ViewModel and setting Textbox in the view accordingly the image and tooltip are shown;

<TextBox Name="txt"
         Grid.Column="0"
         Height="40"
         Background="Aqua"
         Style="{StaticResource textStyle}"
         Text="{Binding Path=Text,
                        UpdateSourceTrigger=PropertyChanged,
                        ValidatesOnDataErrors=True}" />
<TextBlock Grid.Column="1"
           Height="40"
           Background="AliceBlue"
           Text="{Binding ElementName=txt,
                          Path=(Validation.Errors).CurrentItem.ErrorContent}" />

The above code displays correctly in my previous apps and shows the error in the image tooltip as confirmed by the Textblock.

However, in my current app which is built using Prism I can't get the Image to display. The TextBlock updates correctly and I can set the error to the TextBox tooltip via a style trigger without any issue. The problem is I can't seem to get the image (or anything else) to display in the Adorner. The Image is not shown and border is not changed.

The difference between previous apps and this is that the view is in a Region in a ContentControl and I've used dependency injection to inject the viewmodel into the view constructor and set the DataContext.

I can't figure out why this doesn't work when it did previously. I think I may need to include an AdornerDecorator somewhere but I'm perplexed as to where having tried it in a few places without success. Any ideas how I can ensure the Adorner is shown?

1

There are 1 answers

0
pilsdumps On BEST ANSWER

Used an AdornerDecorator to wrap the element containing the texbox and all works fine.