TreeView databinding problems with Silverlight and Caliburn Micro
I have using Silverlight and Caliburn Micro and am having a problem getting child nodes to appear in the tree. The TreeView is contaimed in a grid and here is my XAML excerpt:
<Grid.Resources>
<sdk:HierarchicalDataTemplate x:Key="AccountTemplate">
<TextBlock Text="{Binding AccountNumber}" />
</sdk:HierarchicalDataTemplate>
<sdk:HierarchicalDataTemplate x:Key="CategoryTemplate"
ItemsSource="{Binding CategoryServices}"
ItemTemplate="{StaticResource AccountTemplate}">
<TextBlock Text="{Binding Path=CategoryName}" FontWeight="Bold" />
</sdk:HierarchicalDataTemplate>
</Grid.Resources>
<Controls:TreeView Grid.Row="1" Grid.Column="0"
ItemsSource="{Binding FromAddressServices}"
ItemTemplate="{StaticResource CategoryTemplate}" x:Name="FromTreeView" />
The classes that are bound are:
public class AccountAtAddress
{
public string AccountNumber { get; set; }
}
public class ServiceCategory
{
public string CategoryName { get; set; }
public ObservableCollection<AccountAtAddress> CategoryServices;
}
The problem that I have i开发者_运维百科s that the first level items show but no children. Does anyone have ideas for how to make this work?
You need to make CategoryServices into a public property. You have it defined as a public field and the databinding mechanism doesn't work with fields.
Try the following instead:
<Grid.Resources>
<sdk:HierarchicalDataTemplate DataType="AccountAtAddress">
<TextBlock Text="{Binding AccountNumber}" />
</sdk:HierarchicalDataTemplate>
<sdk:HierarchicalDataTemplate DataType="ServiceCategory"
ItemsSource="{Binding CategoryServices}">
<TextBlock Text="{Binding Path=CategoryName}" FontWeight="Bold" />
</sdk:HierarchicalDataTemplate>
</Grid.Resources>
精彩评论