Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am new to WPF, C#. Also I have never posted a question before. I am using Visual Studio 2013 on Win 7.
I am trying to create a treeview display of objects like so:


Device
      Services
          Variables
          Methods
      Service
          Variables
          Methods
      Device
         Services
             Variables
             Methods
 Device
 etc.....
Service

I have succeeded in displaying the data however I am getting an error in the Debug Output window in the process. The error doesn't seem to affect the display of data. I haven't started refining the look of the treeview. I am focusing on the treeview hierarchical display of the my data first.

I get the following runtime error for each of the DataTemplate classes:

BindingExpression path error: 'Items' property not found on 'object' ''StateVariableViewModel'/'MethodViewModel'

Here is my XAML:
XML
<Window x:Class="DeviceTree.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:DeviceTree.ViewModel"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView Name="tvUPnPNetwork" ItemsSource="{Binding DevViewCollection}"
                  ItemTemplate="{DynamicResource DeviceTemplate}" >
            <TreeView.Resources>
                <!-- Device TEMPLATE -->
                <HierarchicalDataTemplate x:Key="DeviceTemplate" DataType="{x:Type vm:DeviceViewModel}"
	                            ItemsSource="{Binding Items}" >
                    <TextBlock Text="{Binding Name}" />
                </HierarchicalDataTemplate>
                <!-- Service TEMPLATE -->
                <HierarchicalDataTemplate x:Key="ServiceTemplate" DataType="{x:Type vm:ServiceViewModel}">
                    <TextBlock Text="{Binding Name}" VerticalAlignment="Center" />
                </HierarchicalDataTemplate>
                <!-- StateVariable TEMPLATE -->
                <DataTemplate x:Key="StateVarTemplate" DataType="{x:Type vm:StateVariableViewModel}">
                    <TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" />
                </DataTemplate>
                <!-- Method TEMPLATE -->
                <DataTemplate x:Key="MethodTemplate" DataType="{x:Type vm:MethodViewModel}" >
                    <TextBlock Text="{Binding Name}" VerticalAlignment="Center" />
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>


In the DeviceViewModel and the ServiceViewModel I have added 2 different instances of the following method to return each of the two possible collections at their level. The DeviceViewModel method returns Services and subDevices (recursive DeviceViewModels).

This is the method used in the ServiceViewModel.

C#
public IEnumerable<object> Items
 {
     get
     {
         foreach (var state in this._stateVariableCollection)
             yield return state;
         foreach (var method in this._methodCollection)
             yield return method;
     }
 }



Why is the {Binding Items} in the topmost HierarchicalDataTemplateHeirarchicalDataTemplate continuing to search for this itemsource in the lowest DataTemplates?

Answer:
I added the following Items method to each of the two classes in the DataTemplates :StateVariableViewModel, MethodViewModel:

C#
public IEnumerable<object> Items
{ get; set; }


The data is still displayed correctly and I don't get the runtime errors. I would prefer to fix this in the XAML but I don't know how. Is this the proper solution?

A second question I have is how can I provide a tree node with a simple header for each of the 2 non-hierarchical data classes because they get displayed as leaves on the same node. Do I make them hierarchical also?
Posted
Updated 18-Feb-14 9:27am
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900