I created a ViewModel for a TreeView that should read the tree structure from an XML document.

<?xml version="1.0"?> <Tables> <StaticGroups> <StaticGroup Name="StaticGroup1"> <Table> <TableName>TableName1</TableName> <TableTag>TableTag1</TableTag> </Table> <SubGroup Name="SubGroup"> <Table> <TableName>TableName2</TableName> <TableTag>TableTag2</TableTag> </Table> </SubGroup> <SubGroup Name="SubGroup1"> <Table> <TableName>TableName2.1</TableName> <TableTag>TableTag2.1</TableTag> </Table> </SubGroup> </StaticGroup> <StaticGroup Name="StaticGroup2"> <Table> <TableName>TableName3</TableName> <TableTag>TableTag3</TableTag> </Table> </StaticGroup> </StaticGroups> <LoadingConditions> <LoadingCondition Name="Loading Condition"> <Table> <TableName>LoadingTable1</TableName> <TableTag>LoadingTableTag1</TableTag> </Table> </LoadingCondition> </LoadingConditions> </Tables> 

these are classes that are responsible for XML deserialization

  static public class Deserialize { static public NavigationTablesXML NavigationXml() { System.IO.StreamReader file = new System.IO.StreamReader(@"..\..\..\..\NavTables.xml"); var serializer = new XmlSerializer(typeof(NavigationTablesXML)); return (NavigationTablesXML)serializer.Deserialize(file); } } #region Navigation Tables [XmlRoot("Tables")] public class NavigationTablesXML { public NavigationTablesXML() { StaticTables = new List<StaticTablesGroup>(); } public List<TablesGroupBase> RootGroups { get { List<TablesGroupBase> merged = new List<TablesGroupBase>(); if (StaticTables != null) merged.AddRange(StaticTables); if (LoadingConditions != null) merged.AddRange(LoadingConditions); return merged; } } [XmlArray("StaticGroups")] [XmlArrayItem("StaticGroup", typeof(StaticTablesGroup))] public List<StaticTablesGroup> StaticTables { get; set; } [XmlArray("LoadingConditions")] [XmlArrayItem("LoadingCondition", typeof(LoadingTablesGroup))] public List<LoadingTablesGroup> LoadingConditions { get; set; } } #endregion Navigation Tables #region Groups Static & Loading [Serializable] public class TablesGroupBase { public TablesGroupBase() { } [XmlAttribute("Name")] public string Name { get; set; } } public class StaticTablesGroup : TablesGroupBase { public StaticTablesGroup():base() { SubGroups = new List<StaticTablesGroup>(); GroupTables = new List<StaticTable>(); } [XmlElement("SubGroup")] public List<StaticTablesGroup> SubGroups { get; set; } [XmlElement("Table", typeof(StaticTable))] public List<StaticTable> GroupTables { get; set; } } [Serializable] public class LoadingTablesGroup : TablesGroupBase { public LoadingTablesGroup():base() { SubGroups = new List<LoadingTablesGroup>(); GroupTables = new List<LoadingTable>(); } [XmlElement("SubGroup")] public List<LoadingTablesGroup> SubGroups { get; set; } [XmlElement("Table", typeof(LoadingTable))] public List<LoadingTable> GroupTables { get; set; } } #endregion Groups Static & Loading #region Individual Table [Serializable] public class TableBase { [XmlElement("TableName")] public string Name { get; set; } [XmlElement("TableTag")] public string Tag { get; set; } } [Serializable] public class StaticTable:TableBase { } [Serializable] public class LoadingTable : TableBase { } #endregion Individual Table 

my viewmodel which should work with treeview

  public class NavigationTablesXMLViewModel:ViewModelBase { private NavigationTablesXML _tables; public NavigationTablesXMLViewModel() { _tables = Deserialize.NavigationXml(); } public List<TablesGroupBase> TreeData { get { return _tables.RootGroups; } } 

Xaml treeview

 <TreeView ItemsSource="{Binding TablesVM.TreeData}" > <TreeView.Resources> <data:SourceConverter x:Key="conv" /> <HierarchicalDataTemplate ItemsSource="{Binding Path=., Converter={StaticResource conv}}" DataType="{x:Type data:LoadingTablesGroup}"> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsChecked}"/> <TextBlock Text="{Binding Name}"/> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate ItemsSource="{Binding Path=., Converter={StaticResource conv}}" DataType="{x:Type data:StaticTablesGroup}"> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsChecked}"/> <TextBlock Text="{Binding Name}"/> </StackPanel> </HierarchicalDataTemplate> <DataTemplate DataType="{x:Type data:LoadingTable}"> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsChecked}"/> <TextBlock Text="{Binding Name}"/> </StackPanel> </DataTemplate> <DataTemplate DataType="{x:Type data:StaticTable}"> <StackPanel Orientation="Horizontal"> <CheckBox IsChecked="{Binding IsChecked}"/> <TextBlock Text="{Binding Name}"/> </StackPanel> </DataTemplate> </TreeView.Resources> </TreeView> 

How can you make isChecked triggered inside the ViewModel (NavigationTablesXMLViewModel) and when you click on the parent node, all children are marked. I added the isChecked property to the ViewModel (NavigationTablesXMLViewModel) but it certainly does not work

  • Could you reduce the amount of code? Is all this code necessary for understanding the question? - VladD
  • @VladD Of course I can reduce the amount of code, but then, it seems to me, it will not be clear how the ViewModel works. I split the code to make it easier to see. The main question is whether it is possible to get the isChecked event from the TreeView without code behind with a ViewModel like this - Vadim
  • For example, I am not sure that serialization is generally related to your problem. - VladD
  • @VladD You are absolutely right, the problem is most likely not in serialization. My problem is how to build my ViewModel to read data from XML, and to have the same hierarchical structure as TreeView - Vadim
  • What is this problem? Dare? - VladD

0