How to set 2 color background to a TreeView
Hi,
It's not working the way I expect it to. Each nested TreeViewItem starts with AlternationIndex = 0.
Can you tell me how to implement indexing of all expanded TreeViewItems?
Code: Select all
<AlternationConverter x:Key="Converter.AlternationRows">
<SolidColorBrush>#00333333</SolidColorBrush>
<SolidColorBrush>#FF282828</SolidColorBrush>
</AlternationConverter>
<Style TargetType="TreeViewItem">
<Setter Property="AlternationCount" Value="2"/>
<Setter Property="Background"
Value="{Binding Path=(ItemsControl.AlternationIndex),
RelativeSource={RelativeSource Self}, Converter={StaticResource Converter.AlternationRows}}" />
</Style>
<TreeView AlternationCount="2"/>
Can you tell me how to implement indexing of all expanded TreeViewItems?
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: How to set 2 color background to a TreeView
As the TreeView is a hierarchical control, each child has its own Items collection with different indices. If you have to use a TreeView, the AlternationIndex is not an option here, you might want to expose a global index in your item view model instead.
Code: Select all
<Style TargetType="TreeViewItem">
<Setter Property="AlternationCount" Value="2"/>
<Setter Property="Background"
Value="{Binding ItemIndex, Converter={StaticResource Converter.AlternationRows}}"/>
</Style>
Re: How to set 2 color background to a TreeView
I have implemented a Behavior that attaches to the TreeViewItem and
recalculates the AlternationIndex with the Expanded State of the TreeViewItem.
The Behavior handles Loaded, Expanded, and Collapsed.
recalculates the AlternationIndex with the Expanded State of the TreeViewItem.
The Behavior handles Loaded, Expanded, and Collapsed.
-
- KeldorKatarn
- Posts: 234
- Joined:
Re: How to set 2 color background to a TreeView
When it doubt.. Behavior ;) Works almost always for these kinds of things in XAMLI have implemented a Behavior that attaches to the TreeViewItem and
recalculates the AlternationIndex with the Expanded State of the TreeViewItem.
The Behavior handles Loaded, Expanded, and Collapsed.
Re: How to set 2 color background to a TreeView
That's for sureWhen it doubt.. Behavior ;) Works almost always for these kinds of things in XAML
-
-
sfernandez
Site Admin
- Posts: 3197
- Joined:
Re: How to set 2 color background to a TreeView
That is a good solution too, thanks for sharing it!I have implemented a Behavior that attaches to the TreeViewItem and
recalculates the AlternationIndex with the Expanded State of the TreeViewItem.
The Behavior handles Loaded, Expanded, and Collapsed.
Who is online
Users browsing this forum: MarioBarbero, Semrush [Bot] and 1 guest