I've got a TreeView that is constructed like this:
//This is for dynamically building a treeview with templates from an XML file
XmlTextReader xmlReader1 = new XmlTextReader("HierarchicalDataTemplate1.xml");
HierarchicalDataTemplate hierarchicalDataTemplate1 = XamlReader.Load(xmlReader1) as HierarchicalDataTemplate;
And it reads an XML file like this:
<HierarchicalDataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ItemsSource="{Binding XPath=SubCategory}">
<TextBlock FontSize="36" FontFamily="K22 Monastic" Text="{Binding XPath=#CategoryName}" />
<Button>Add Subordinate Unit</Button>
</HierarchicalDataTemplate>
But it throws a runtime error on adding the button:
''Template' property has already been set on 'HierarchicalDataTemplate'.' Line number '3' and line position '4'.
Is what I'm trying to do possible? If I take out the script for adding a button everything works fine.
Thanks!
One obvious error is that you've got two elements at the root level of the template's visual tree. You can't do that. A DataTemplate or HierarchicalDataTemplate can have only one child. So your first step is to make that one child a control that supports multiple children of its own, then put your TextBlock and your Button in that. StackPanel is a good one:
<HierarchicalDataTemplate
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
ItemsSource="{Binding XPath=SubCategory}"
>
<StackPanel Orientation="Horizontal">
<TextBlock
FontSize="36"
FontFamily="K22 Monastic"
Text="{Binding XPath=#CategoryName}"
/>
<Button>Add Subordinate Unit</Button>
</StackPanel>
</HierarchicalDataTemplate>
It's interesting to note that when I paste your template XAML into the XAML designer, I get a different error: "The property 'VisualTree' is set more than once" -- but when I duplicate your XamlReader.Load(), code, I get the same exception and message as you (and the same fix corrects it).
Google turns up zero results for "Template property has already been set on HierarchicalDataTemplate". Well, maybe it'll have one now.
Related
I've to create a custom tree structure which should looks like:
-Start
--Process
--Decision
---DecisionOption
----Process
----Process
---DecisionOption
----Process
Each of these Types has a set of custom attributes. Basically a TreeViewItem structure works fine and the 'hierarquical facilities' (like get parent and childs) within is a plus.
What is 'best' way to do it?
I did some ways but I'm having problem when trying to apply HierarquicalDataTemplates. (e.g.). It simply doesn't work.
<HierarchicalDataTemplate DataType="{x:Type local:Start}" ItemsSource="{Binding Items}">
<Border >
<TextBlock Text="{Binding Path=Title}" />
</Border>
</HierarchicalDataTemplate>
Any ideias how to implement that structure?
This shows the multi-tiered Treeview -- Here is a step by step usage of HierarchicalDataTemplate
As Mike indicates in the second article the HDT does not exactly program itself. Follow his steps for using it to create an East/West conference list and you should have your answer as what to do.
If I use the following code without <DataTemplate> it is working and I can see only the SoftwareVersion, when I add <DataTemple> throws an exception. Any idea why?
ObservableCollection<Note> AllNotes
<TreeView Name="tree" ItemsSource="{Binding Path=AllNotes}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type m:Note}" ItemsSource="{Binding Path=ListIssuesType}">
<TextBlock Text="{Binding Path=SoftwareVersion}" Margin="2" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<DataTemplate DataType="{x:Type m:IssueType}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=IssueTypeName}" />
</StackPanel>
</DataTemplate>
</TreeView>
It is not clear at all why you are adding a DataTemplate to your control. But it is clear why you are getting the exception: you have added the DataTemplate object as a direct child of the TreeView, which makes it an item in the Items collection. It's "template" nature becomes completely irrelevant; you are telling the control to just include that object as an element in the view.
And of course, now that the Items collection is non-empty, you are not allowed to send the ItemsSource property.
So the fix for the exception is "don't do that". I.e. stop trying to make the DataTemplate element an item in the tree view's list of items.
Beyond that, there's not enough context in your question to say how you should be using the DataTemplate object. Presumably you intend it to be used as part of the template for an actual data item in the tree view; maybe it needs to be incorporated into the HierarchicalDataTemplate somehow.
But without more specifics regarding exactly what the code does now and what you want it to do instead, as well as you providing a good Minimal, Complete, and Verifiable example clearly illustrating your scenario, I don't see a good way to address that part of your problem. If you still need help with defining the data template, please post a new question that provides the necessary details.
I have a tree view of items, which currently shows all the levels of children, down to the furthest. How can I achieve to show only the first level of children? Is the HierarchicalDataTemplate the wrong approach, perhaps? Collapsing the children of level 2 and further would not be sufficent.
How about using a filtered version of your datasource so only the levels you want are included, then you can use a HierarchialDataTemplate without any problem.
Just use a DataTemplate, instead of the HierarchicalDataTemplate.
Edit: Got it. There's a number of options. Tommy's recommendation above is a good one and elegant. Another option is to override the TreeViewItem's ControlTemplate for any item whose children you don't want to see and hide the expander area.
Well, without manipulating the data it's not possible to just show one level of children. The control would have needed a property, which is able to determine the deepness of shown nodes.
This solution came to me and was quite obvious: I just use two flat tree views, the second one dependent on the SelectedItem of the first one. No HierarchicalDataTemplate needed, at all. Just a common DataTemplate.
As I understand it, you want the top level nodes, and 1 level of children of those, and no further (so there will be 2 levels of nodes overall). Then you can do it with 2 templates if you want to do it in XAML:
<Grid>
<Grid.Resources>
<DataTemplate x:Key="TemplateLeaf">
<TextBlock Text="{Binding Text}" /> <!-- Whatever leaf view you want -->
</DataTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding Items}" ItemTemplate="{StaticResource TemplateLeaf}" x:Key="TemplateNode">
<TextBlock Text="{Binding Text}" />
</sdk:HierarchicalDataTemplate>
</Grid.Resources>
<sdk:TreeView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource TemplateNode}" />
</Grid>
(That's the Silverlight version but it's the same). By default the HierarchicalDataTemplate uses itself as its own ItemTemplate, but you can replace that with some other template for the next level (including a plain DataTemplate) if you want.
I have a TreeView databound to a CollectionViewSource Groups collection. This is so that I can display the data using the power of the CollectionViewSource, and the data itself also has a hierarchical structure, which is why I need the TreeView. I have a second control which is bound to the SelectedItem of the TreeView.
The problem is that when the group header is selected, the program crashes with the following exception.
{"A TwoWay or OneWayToSource binding cannot work on the read-only property 'Name' of type 'MS.Internal.Data.CollectionViewGroupInternal'."}
The objects in my TreeView contain a Name property that is two way bound in another control. The binding engine seems to find the Name property for the Group and attempt to bind to that. How can I prevent this exception from occuring? I would like for the rest of my program to treat it as if nothing is selected when the group header is selected, or disallow selecting the group header all together. Below is a simplified version of the code.
<TreeView
x:Name="CustomersTree"
ItemsSource="{Binding CustomersViewSource.Groups}"
ItemTemplate="{StaticResource CustomerGroupsTemplate}">
<MyUserControl DataContext="{Binding ElementName=CustomersTree, Path=SelectedItem, Mode=OneWay}" />
<HierarchicalDataTemplate x:Key="CustomerGroupsTemplate" ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource CustomerTreeItemTemplate}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="CustomerTreeItemTemplate" ItemsSource="{Binding Customers}">
<StackPanel>
<Image Source="{Binding ImageSource}" />
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
To be clear, the error is a result of the binding in the CustomerGroupsTemplate as far as I can tell, and changing this binding to OneWay results in the same error. The information in the tree shows up the way it is expected to, it is only when a group header is selected that the exception occurs.
The problem was the result of a two way binding within the user control. I ended up using a converter to check for the type of the object being bound, ignoring it if it wasn't the object I wanted.
To begin I would like to apologize for my english which is far from perfect (its not my native language...).
I've a problem related to databinding in my XAML code. I've a combox which is supposed to list all graphical nodes that I drop on a custom canvas. My graphical nodes are referenced in a list graphCanvas.uinodes , and each node has a name. And that's what I want to show in my combobox.
So I tried something like this:
<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes/name}"
Height="23" HorizontalAlignment="Left" Name="comboBox1"
VerticalAlignment="Top" Width="71" Foreground="Black" />
But even after drawing nodes on my canvas my combox is empty...
Any clue?
Thanks.
A binding using ElementName finds the WPF element with that name. I doubt that you've subclassed Canvas and added a uinodes property to it, which is the only way that Path would find something even if the path syntax were correct, which it's not.
If you look in the Output window when you run your program, you'll see an error message that tells you why the binding isn't working. That's a start.
But even then, you won't get what you want with this approach. What you probably want looks more like:
<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}"
DisplayMemberPath="name"/>
or even
<ComboBox ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}">
<ComboBox.ItemTemplate>
<TextBlock Text="{Binding name}"/>
</ComboBox.ItemTemplate>
</ComboBox>
Your binding (specifically the Path assignment) looks wrong. Assuming uinodes is an Enumerable of some sort it looks as though you are trying to bind to the `name' property of the collection, which does not exist. Try this:
ItemsSource="{Binding ElementName=graphCanvas, Path=uinodes}" DisplayMemberPath="name"
As an aside, you can use the output window to see any binding errors.