I have some data in my view-model, which looks roughly like this:
public class CostItem
{
public string Description { get; set; }
public int Quantity { get; set; }
public double UnitCost { get; set; }
}
public class Section
{
public string Name { get; set; }
List<CostItem> CostItems { get; }
}
I am displaying it using the following TreeView:
<TreeView x:Name="TreeViewBreakdown" ItemsSource="{Binding Sections}"
AlternationCount="2">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type me:Section}" ItemsSource="{Binding CostItems}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type me:CostItem}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Grid.Row="0" Grid.Column="0" Text="{Binding Description}"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Quantity}"/>
<TextBox Grid.Row="0" Grid.Column="2" Text="{Binding UnitCost}"/>
</Grid>
</DataTemplate>
</TreeView.Resources>
</TreeView>
What I would like to do is apply and access an
AlternationIndex
to the
CostItem
child nodes displayed by this
TreeView
, so that I can alternate the background colour. Is there any way to do this?
Additional Note:
Having dug deeper into this, I think there is one central thing I am failing to grasp. I have declared two templates in my
TreeView
, one hierarchical and one normal, and these are chosen by type of object being bound to. All good so far.
In the case of the normal
DataTemplate in my XAML, it is my understanding that this is the template applied
to each child element. What I actually want to be able to say to the
TreeView
is something like, "When you are binding to my parent-class, I want you to use, say, a
ListView
or a
DataGrid
for all of the children of this parent." It is difficult to explain, but I'd be very grateful for clarification of this central point (if anyone understands what tosh I'm babbling on about)?
What I have tried:
It seems that what I need is this (taken from
datagrid - How to make gridview a child element of a treeview in wpf application - Stack Overflow[
^] ):
public class Customer
{
public string Name { get; set; }
public IEnumerable<Order> Orders { get; set; }
}
public class Order
{
public DateTime PurchaseDate { get; set; }
public IEnumerable<OrderItem> Items { get; set; }
}
public class OrderItem
{
public string ProductName { get; set; }
public int Quantity { get; set; }
public double UnitPrice { get; set; }
public double TotalPrice { get; set; }
}
<TreeView x:Name="_treeView" ItemsSource="{Binding}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type data:Customer}"
ItemsSource="{Binding Path=Orders}">
<TextBlock Text="{Binding Name}"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type data:Order}">
<StackPanel>
<TextBlock Text="{Binding PurchaseDate}"/>
<ListView ItemsSource="{Binding Items}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding ProductName}" />
<GridViewColumn DisplayMemberBinding="{Binding Quantity}" />
<GridViewColumn DisplayMemberBinding="{Binding UnitPrice}" />
<GridViewColumn DisplayMemberBinding="{Binding TotalPrice}" />
</GridView>
</ListView.View>
</ListView>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
Except what I need to do is chop off the top level of this tree (i.e. I don't need the
Customer
level. I get the feeling I'm being dense but I just don't seem to be able to work out the syntax to do this. Because I only have two levels, parent and child, do I even need a
HierarchicalDataTemplate
? Is that where I am going wrong?