2
votes

I have a TreeView in my WPF application. In runtime I am binding data to the Treeview. Each node in a treeview is associated with path. I should change the color of the TreeView element If the file in the path associated with the element has an error. Say I need to change it to RED.

So Since I am binding it on fly after the treeview is entirely loaded I should traverse the tree again and I should check the path contains any error for every element in the TreeView.

How can I navigate the entire Tree element one by one from parent to root child and perform the error checking operation for each node.

Tree in XAML:

 <TreeView Grid.Column="0" Grid.Row="0"  HorizontalAlignment="Stretch" Name="treeView1" 
                  VerticalAlignment="Stretch"
                  SelectedItemChanged="treeView1_SelectedItemChanged" HorizontalContentAlignment="Stretch" 
                  VerticalContentAlignment="Top" BorderThickness="0,0,0,1" BorderBrush="LightGray">

                <TreeViewItem Header="Head Tree" ItemsSource="{Binding MainComps}">
                    <TreeViewItem.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}">
                            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                            <Setter Property="FontWeight" Value="Normal" />
                          <Style.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="FontWeight" Value="Bold" />
                                </Trigger>
                                <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                    <Setter Property="Foreground" Value="RED" />
                                </DataTrigger>
                            </Style.Triggers>

                        </Style>
                    </TreeViewItem.ItemContainerStyle>
                    <TreeViewItem.Resources>
                        <HierarchicalDataTemplate  DataType="{x:Type TextBlock}" ItemsSource="{Binding Children}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Head Tree" />
                            </StackPanel>
                        </HierarchicalDataTemplate>
                        <HierarchicalDataTemplate DataType="{x:Type local:MainCompViewModel}" ItemsSource="{Binding Children}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Maincompname}" />
                            </StackPanel>
                        </HierarchicalDataTemplate>
                        <HierarchicalDataTemplate DataType="{x:Type local:FeatureViewModel}" ItemsSource="{Binding Children}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding FeatureName}" />
                            </StackPanel>
                        </HierarchicalDataTemplate>
                        <DataTemplate DataType="{x:Type local:CompViewModel}">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Component}" />
                            </StackPanel>
                        </DataTemplate>

                    </TreeViewItem.Resources>

                </TreeViewItem>
            </TreeView>

In code behind:

I have treeview selection changed event. How can I have AfterTreeViewLoaded event. Something like this.

Void TreeviewLoaded()
{
//Identify error. Change the color
}

Thanks in advance..

1

1 Answers

2
votes

When you create tree items you should have some list of root items and bind the list to your TreeView.ItemsSource.

Then at any time you can check the paths and set boolean HasError (the property should exist in your ItemViewModel). Tree item's backround can be changed by style, like this:

<Setter Property="Background" Value="{Binding HasError, Converter={StaticResource HasErrorToBackroundConverter}" />

UPD:

Please see the following article http://blog.clauskonrad.net/2011/04/how-to-make-hierarchical-treeview.html.

Your ItemViewModel is just like the Folder from the article. The list of root items is m_folders. Like the Folder contains a FullPath, your ItemViewModel will contain Path alongside HasError property.

When you need to check the path, you recursively pass m_folders, read the path from it, check it and set HasError.