Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
In a WPF application I am using a TreeView and it is bind with a source. I am using a DataTrigger to change the background of TreeViewItem based on certain condition. Here my DataTrigger Code

HTML
<DataTrigger Binding="{Binding Id, Converter={StaticResource TreeViewItemBackGroundConverter}}" Value="True">
                               <Setter Property="Background" Value="Red"/>
                               <Setter Property="Opacity" Value="0.75"/>
                               <Setter Property="ToolTip" Value="{Binding Id, Converter={StaticResource ErrorConverter}}"/>
                           </DataTrigger>


It Works perfect when loading but now I want to do it on selection change or items property change. I want to check the same condition that I am using with Data Trigger above to selected Item and based on condition I want to change the background.

I tried to use SelectedItemChanged event but not able to find the TreeViewItem. I am using a ItemTemplate for the TreeViewItem. How I can do this?

Please help me.
Posted
Updated 18-Jan-15 22:51pm
v2
Comments
gggustafson 16-Jan-15 10:03am    
A little code might help us help you.
Vi(ky 19-Jan-15 4:40am    
Thanks for your consideration :)
Please let me know what you need to understand.
gggustafson 19-Jan-15 10:15am    
Let's start with your SelectedItemChanged event handler.

1 solution

You need to change the ItemContainerStyle of the TreeView in xaml. Bind the Background property to a string property in the item view model and use a converter to convert that string property into a Brush. You can then set the background from the view model when the IsSelected property is set. Here are some code snippets that may be useful.


C#
<treeview.itemcontainerstyle>
      <style targettype="{x:Type TreeViewItem}">
       <setter property="Background" value="{Binding BackgroundColour, Converter={StaticResource BackgroundToColourConverter},Mode=OneWay}" />
       <setter property="IsExpanded" value="{Binding IsExpanded, Mode=TwoWay}" />
        <setter property="IsSelected" value="{Binding IsSelected, Mode=TwoWay}" />
       </style>
    </treeview.itemcontainerstyle>

 public class BackgroundToColourConverter : IValueConverter
   {
       public object Convert(object value, Type targetType,
    object parameter, CultureInfo culture)
       {
           string s = value as string;
         Color color;
           switch (s)
           {
               case "Green":
                   color = Colors.Green;
                   break;
               case "Red":
                   color = Colors.Red;
                   break;
               default:
                   color = Colors.White;
                   break;

           }
           return new SolidColorBrush(color);
       }

       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
          return null;
       }
   }
 
Share this answer
 
Comments
Vi(ky 19-Jan-15 4:37am    
Thanks for reply. I use your code and it works but not as I want. Actually problem is In my Treeview some TreeViewItems are invalid. I show those invalid items with red background & I done it but when user add a new TreeViewItem from UI (there is option to add) or user can edit the TreeView Item values. I need mechanism that change background color of TreeView Item based on validation. I already written the code for validation but I can't understand how to change item background when user edit or add new items?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900