Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Using HierarchicalDataTemplates in a treeview with multiple levels using multiple ObservableCollections

$
0
0

I am new to WPF, C#. Also I have never posted a question before. I am trying to create a treeview display of objects like so:

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

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.

The error I get is:

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

XAML

    <Grid>
        <Grid.Resources>
            <!-- Device TEMPLATE -->
            <HierarchicalDataTemplate x:Key="DeviceTemplate" DataType="{x:Type vm:DeviceViewModel}"
                             ItemsSource="{Binding Items}" >
                <StackPanel Orientation="Horizontal">
                    <CheckBox Focusable="False" Margin="5" Visibility="Collapsed"
                     IsChecked="{Binding IsChecked,Mode=TwoWay}"
                     VerticalAlignment="Center" />
                    <TextBlock Text="{Binding Name}"
                     Margin="5 5 10 10" />
                </StackPanel>
            </HierarchicalDataTemplate>
            <!-- Service TEMPLATE -->
            <HierarchicalDataTemplate x:Key="ServiceTemplate" DataType="{x:Type vm:ServiceViewModel}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox Focusable="False" Visibility="Collapsed"
                       IsChecked="{Binding IsChecked,Mode=TwoWay}"
                       VerticalAlignment="Center" Margin="5" />
                    <TextBlock Text="{Binding Name}"
                        VerticalAlignment="Center" Margin="5" />
                </StackPanel>
            </HierarchicalDataTemplate>
            <!-- StateVariable TEMPLATE -->
            <DataTemplate x:Key="StateVarTemplate" DataType="{x:Type vm:StateVariableViewModel}">
                <StackPanel Orientation="Horizontal">
                    <CheckBox Focusable="False" Visibility="Collapsed"
                       IsChecked="{Binding IsChecked,Mode=TwoWay}"
                       VerticalAlignment="Center" Margin="5" />
                    <TextBlock Text="{Binding Path=Name}"
                        VerticalAlignment="Center" Margin="5" />
                </StackPanel>
            </DataTemplate>
            <!-- Method TEMPLATE -->
            <DataTemplate x:Key="MethodTemplate" DataType="{x:Type vm:MethodViewModel}" >
                <StackPanel Orientation="Horizontal">
                    <CheckBox Focusable="False" Visibility="Collapsed"
                       IsChecked="{Binding IsChecked,Mode=TwoWay}"
                       VerticalAlignment="Center" Margin="5" />
                    <TextBlock Text="{Binding Name}"
                        VerticalAlignment="Center" Margin="5" />
                </StackPanel>
            </DataTemplate>
        </Grid.Resources>
        <TreeView ItemsSource="{Binding DevViewCollection}"
                  ItemTemplate="{StaticResource DeviceTemplate}" >
        </TreeView>
    </Grid>

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.

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

Neither of these two collections listed in the above method has the Items property because they have only one collection each and are the lowest element of the treeview. Why is the {Binding Items} in the topmost

HierarchicalDataTemplateHeirarchicalDataTemplate continuing to search for the ItemsSource="{Binding Items}" in the lowest DataTemplates?





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>