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:
<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>
<HierarchicalDataTemplate x:Key="DeviceTemplate" DataType="{x:Type vm:DeviceViewModel}"
ItemsSource="{Binding Items}" >
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="ServiceTemplate" DataType="{x:Type vm:ServiceViewModel}">
<TextBlock Text="{Binding Name}" VerticalAlignment="Center" />
</HierarchicalDataTemplate>
<DataTemplate x:Key="StateVarTemplate" DataType="{x:Type vm:StateVariableViewModel}">
<TextBlock Text="{Binding Path=Name}" VerticalAlignment="Center" />
</DataTemplate>
<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.
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:
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?