Hi All,
I'm quite new to programming and I am trying to bind some data stored in Sybase DB to a treeView in WPF. My data consists of information about PartyGroups and for each PartyGroup there are Persons associated with that group. So far I have managed to bind the data so that it displays a list of PartyGroups in the treeView but cannot get my head around hierarchical data templates i.e how to link the Persons. Would appreciate any help, many thanks!
Ben
I'm quite new to programming and I am trying to bind some data stored in Sybase DB to a treeView in WPF. My data consists of information about PartyGroups and for each PartyGroup there are Persons associated with that group. So far I have managed to bind the data so that it displays a list of PartyGroups in the treeView but cannot get my head around hierarchical data templates i.e how to link the Persons. Would appreciate any help, many thanks!
Ben
Code:
//Entity Class
public class PartyGroup
{
public int PersonID { get; set; }
public string FullName { get; set; }
public int PartyID { get; set; }
public string PartyName { get; set; }
public List<PartyGroup> PartyGroupList { get; set; }
}
//Database
private List<PartyGroup> myPartyGroupList;
public IEnumerable<PartyGroup> PartyGroupList
{
get { return myPartyGroupList; }
}
using (AseDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
PartyGroup partyGroup = new PartyGroup();
partyGroup.PersonID = Convert.ToInt32(reader["person_id"]);
partyGroup.FullName = reader["full_name"].ToString();
partyGroup.PartyID = Convert.ToInt32(reader["party_group_id"]);
partyGroup.PartyName = reader["party_group_name"].ToString();
myPartyGroupList.Add(partyGroup);
}
}
//XAML
<TreeView x:Name="treeview" Margin="131,129,203,29" ItemsSource="{Binding PartyGroupList, Mode=Default}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:PartyGroup}"
ItemsSource="{Binding PartyGroupList}">
<StackPanel>
<TextBlock Text="{Binding PartyName, Mode=Default}"/>
</StackPanel>
</HierarchicalDataTemplate>
<!--<HierarchicalDataTemplate DataType="{x:Type PartyGroupList}">??
<StackPanel>
<TextBlock Text="{Binding FullName, Mode=Default}"/>
</StackPanel>
</HierarchicalDataTemplate>-->
</TreeView.Resources>
</TreeView>