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 7494773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:46:21+00:00 2026-05-29T17:46:21+00:00

I’m trying to validate items inside a treeview. The main idea is that a

  • 0

I’m trying to validate items inside a treeview. The main idea is that a user selects an object from the tree and that loads its details which can be edited. That’s all working fine as I’ve got INotifyPropertyChanged hooked up but… my model has validation logic hooked up (IDataErrorInfo) and I’d like to show it in the treeview as well (highlight the item(s) which have validation errors).

I’ve tried some things already but I just can’t figure out where to put the Validation in the binding to make it work like I would like it to.

My Validation ControlTemplate:

<ControlTemplate x:Key="validationTemplate">
            <StackPanel Orientation="Horizontal">
                <AdornedElementPlaceholder x:Name="MyAdorner" />
                <Image
                                            MaxHeight="{Binding ElementName=MyAdorner, Path=ActualHeight}" 
                                            MaxWidth="20"
                                            Source="{Binding Source={StaticResource ValidationIcon}, Converter={StaticResource UriConverter}}" 
                                            Margin="1" RenderOptions.BitmapScalingMode="HighQuality"
                                            VerticalAlignment="Center" HorizontalAlignment="Center" />
            </StackPanel>
        </ControlTemplate>

The treeView:

<TreeView ItemsSource="{Binding Path=ProductCategories}"
              Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged">           
                <TreeView.ItemTemplate>
                    <HierarchicalDataTemplate DataType="{x:Type data:ProductCategory}"
                                      ItemsSource="{Binding Path=ProductCategories}">
                        <StackPanel Orientation="Horizontal">
                            <StackPanel.Style>
                                <Style TargetType="StackPanel">
                                    <Style.Triggers>
                                        <Trigger Property="Validation.HasError" Value="True">
                                            <Setter Property="ToolTip"
                                                    Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                                            <Setter Property="Validation.ErrorTemplate"
                                                    Value="{StaticResource validationTemplate}" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </StackPanel.Style>
                            <StackPanel.BindingGroup>
                                <BindingGroup />
                            </StackPanel.BindingGroup>
                            <StackPanel.ToolTip>
                                <TextBlock Margin="2" Text="{Binding Path=Description}" />
                            </StackPanel.ToolTip>
                            <TextBlock Text="{Binding Path=Name}" FontSize="10" FontWeight="Medium" />
                            <TextBlock Text="{Binding Path=ProductCount, StringFormat='   ({0})'}" />
                        </StackPanel>
                    </HierarchicalDataTemplate>
                </TreeView.ItemTemplate>
            </TreeView>

Basically I’m trying to put a little icon next to the item in the treeview if the underlying Model has a validation error.

I’ve tried playing around with BindingGroup but that’s a new topic for me so I’m not sure if that is even the way to go.

Any ideas?

  • 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-05-29T17:46:24+00:00Added an answer on May 29, 2026 at 5:46 pm

    First, you need a couple of properties in your data class like the following:

    public ObservableCollection<string> Errors
    {
        get
        {
            ObservableCollection<string> errors = new ObservableCollection<string>();
            errors.AddUniqueIfNotEmpty(this["PropertyToValidate1"]);
            errors.AddUniqueIfNotEmpty(this["PropertyToValidate2"]);
            ...
            errors.AddUniqueIfNotEmpty(this["PropertyToValidateN"]);
            return errors;
        }
    }
    

    This first one calls the indexer added for IDataErrorInfo and adds all validation errors into the collection if they are not already in there. The AddUniqueIfNotEmpty method is an extension method that ‘does what it says on the tin’. I trust you can manage this part for yourself. The nice thing about this collection is that you can bind to it and see a collection of every validation error, not just one at a time like with the simple IDataErrorInfo implementation.

    public bool HasError
    {
        get { return Errors != null && Errors.Count > 0; }
    }
    

    The second property simply returns a bool that signifies whether there are any validation errors in the data object or not.

    Then, you can simply bind to this property in your Template or Style. Here is an example that would change the colour of a Border if the relating data object had any validation errors.

    <Style x:Key="ValidationBorderStyle" TargetType="{x:Type Border}">
        <Setter Property="Border.BorderBrush" Value="Black" />
        <Style.Triggers>
            <DataTrigger Binding="{Binding HasError, FallbackValue=False}" Value="True">
                <Setter Property="Border.BorderBrush" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.