Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9230437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:49:31+00:00 2026-06-18T05:49:31+00:00

I am currently working on an MVVM Solution, using WPF Validation. What I’d like

  • 0

I am currently working on an MVVM Solution, using WPF Validation.

What I’d like to do is be able to control when the Validation Adorners are shown using a “ShowErrors” Visibility Property in my Context.

I have the following Template for the WPF ComboBox Validation Adorners, contained within my Application.xaml file;

<Style TargetType="{x:Type ComboBox}">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="Margin" Value="0,2,40,2" />
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="true">
                            <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                            ToolTip="{Binding ElementName=customAdorner1, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white">
                                </TextBlock>
                            </Border>
                            <AdornedElementPlaceholder Name="customAdorner1" VerticalAlignment="Center" >
                                <Border BorderBrush="red" BorderThickness="1" />
                            </AdornedElementPlaceholder>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

(I have a separate Template for TextBoxes)

After doing some searching on StackOverflow and Google, I tried adding the following to the DockPanel;

Visibility="{Binding DataContext.ShowErrors, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay}"

But this doesn’t seem to work for me, even though the same method works fine from within the Main XAML…. Any ideas?

EDIT: My UserControl to which I bind my DataContext to has an x:Name of “MainContext”

I found acouple of theads which suggested;

Visibility="{Binding DataContext.ShowErrors, Source={x:Reference Name=MainContext}, Mode=TwoWay}">

Which gives an error of

Unresolved reference ‘MainContext’

Aswell as;

Visibility="{Binding DataContext.ShowErrors, ElementName=MainContext, Mode=TwoWay}">

Which just doesn’t work.

Edit2: If I move the whole adorner out of the Appliation.xaml and into the UserControl Resources, then use;

Visibility="{Binding DataContext.ShowErrors, ElementName=MainContext, Mode=TwoWay}">

It works… Not ideal though, as I don’t want to have to repeat the Template across all my Screens

Edit 3: Ok, so I’ve found a workaround for now. I used the following for the Visibility Binding…

Visibility="{Binding ElementName=customAdorner1, Path=AdornedElement.Parent.DataContext.ShowErrors, Converter={StaticResource MyBolVisibilityConverter}, Mode=TwoWay}"

I then added a Boolean ShowErrors Property to the context and added a Converter to convert from the Boolean Value to a Visibility Value, mainly because of where the ShowErrors Property actually ended up.

This was slightly more confusing as my Form is a Master Details arrangement, where the Adorners are shown within the details section, which has it’s own DataContext. This, of course, doesn’t have access to the UserControl’s DataContext directly.

This seems like a bit of a hack to me really, so I would appreciate a better solution!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:49:32+00:00Added an answer on June 18, 2026 at 5:49 am

    I solved this by adding a ShowErrors Boolean Property to my DataContext, then Binding the Adorner Visibility to the AdornedElement’s Parents DataContext, which of course if my known DataContext.

    I used the following XAML in my Application.xaml file;

    <Converters:BolVisibilityConverter x:Key="MyBolVisibilityConverter"/>
    <Style TargetType="{x:Type TextBox}">
          <Setter Property="VerticalAlignment" Value="Center" />
          <Setter Property="Margin" Value="0,2,40,2" />
          <Setter Property="Validation.ErrorTemplate">
              <Setter.Value>
                  <ControlTemplate>
                            <DockPanel LastChildFill="true" Visibility="{Binding ElementName=customAdorner, Path=AdornedElement.Parent.DataContext.ShowErrors, Converter={StaticResource MyBolVisibilityConverter}, Mode=TwoWay}">
                                <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                                ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                    <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white">
                                    </TextBlock>
                              </Border>
                              <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
                             <Border BorderBrush="red" BorderThickness="1" />
                           </AdornedElementPlaceholder>
                       </DockPanel>
                   </ControlTemplate>
               </Setter.Value>
          </Setter>
    </Style>
    

    I used the following Converter Code;

    Namespace Converters
    
        Public Class BolVisibilityConverter
            Implements IValueConverter
    
            Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
    
                If value Is Nothing OrElse value = False Then
    
                    Return Visibility.Hidden
    
                Else
    
                    Return Visibility.Visible
    
                End If
    
            End Function
    
            Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
                Return DirectCast(value, Boolean)
    
            End Function
        End Class
    
    End Namespace
    

    I then simply set the ShowErrors Property in my DataContext, to either True or False in order to Show or Hide the Adorners.

    This was very useful, as the Adorners always appear on the very top layer, and so show up over the top of Custom Dialog boxes etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hy guys! I am currently working on a little WPF project using MVVM via
I am currently working on an application using WPF and MVVM. Now if I
I’m currently working on a WPF MVVM application using MVVM Light as the MVVM
Currently working in Java, i'd like to be able to select part of an
I'm currently working on an app using MVVM that needs to have some data
I'm currently working on a WPF application that uses MVVM. I've got a ListBox
I'm working in WPF using the MVVM pattern, and generally things seem to be
I am working on a recreation of a search control using the MVVM pattern.
I started working on a WPF project using Prism and MVVM, and I am
I am currently working on a project of mine which is a MSPaint-like WPF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.