This is a migrated thread and some comments may be shown as answers.

Refresh template

6 Answers 118 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 01 Aug 2009, 11:37 PM
Hi,

I'm setting a template (more or less) in the same way as described here: http://www.telerik.com/community/forums/silverlight/treeview/binding-checkboxes-in-xaml.aspx#808822

Once a node has decided its template, it seems to be fixed, but when properties of my nodes change I need the template to change. Is there a way of telling the treeview to reset its templates?

Thanks, James.

6 Answers, 1 is accepted

Sort by
0
Accepted
Miroslav
Telerik team
answered on 03 Aug 2009, 08:01 AM
Hi James,

No, In Silverlight there is no standard way to do this. We were considering to allow ItemTemplate "refresh" but it will involve a lot of code and care (the issue here is that you can set a template in many ways and it is not easy to distinguish where a particular value came from). If there is enough demand we will certainly look into this.

Now, here are my suggestions:
1. If you want to drastically change the template, then you may indeed need and ItemTemplate refresh. A dirty workaround here is to reset the ItemsSource of the TreeView (set it to null and then to whatever was set before). I would advice to do this only for very small TreeViews when the ItemsSource is not bound, basically avoid this.

2. If you want a visual part to appear/disappear then you can bind its Visibility to a property in the ViewModel. In your link you need to show CheckBoxes if some property is greater than a certain value. Then you can create an IntToVisibility converter and bind the Visibility of the CheckBox to this property. If you take this further, you can customize the template at runtime quite a lot without changing it.

3. Please note that there are CotnainerBindings for our ItemTemplates in Silverlight. They work similarly to the Style Bindings in WPF, but they are declared as an attached property of the DataTemplate rather than the Style. With them you can bind properties of the ItemContainer (i.e. TreeViewItem) to the ViewModel. There you can bind the HeaderTemplate of the TreeViewItem to a property of the ViewModel. Updating the HeaderTemplate, should update the visuals as well. Again, this is a bit hard to maintain.

The most "natural" way to achieve what you want is the 2nd suggestion, hopefully it will work in your case. I can be more specific if you need more clarifications or an example.

Kind regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Frank
Top achievements
Rank 1
answered on 03 Aug 2009, 02:09 PM
I currently have two HierarchicalDataTemplates in a DataTemplatesProvider class and use a DataTempaltesSelector to set the appropriate template on the tree view item based on the item type.  Works great.

I want to add the ability to change the template applied to the tree view item dynamically.  That is, I want to add a button to the UI and when clicked I want to change the template used for the tree view items to allow the user to get a different view of the items in the tree.

From your reply above I am understanding that this is not possible.  Your suggested workarounds seem to provide behavior that is close to this, but not quite the same.  Is there currently no way to change the data template of a tree view item dynamically?
0
Miroslav
Telerik team
answered on 03 Aug 2009, 02:24 PM
Hello Frank,

No, there is no way direct way to change them dynamically. Again - you can reset the items source, or bind elements in the template to properties.

If you need to show a different ItemsSource for the items (i.e. changing the hierarchy), then presently you can only reset the items source.

Regards,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Frank
Top achievements
Rank 1
answered on 03 Aug 2009, 02:46 PM
Could you provide an example of your suggested workaround #2? 

"2. If you want a visual part to appear/disappear then you can bind its Visibility to a propery in the ViewModel.  In your link you need to show CheckBoxes if some property is greate than a certain value.  Then you can create an IntToVisiblity converter and bind the Visibility of the CheckBox to this property.  If you take this further, you can customize the template at runtime quite a lot without changing it."

That approach sounds like it may provide functionality very similar to what I am trying to implement. 
0
Accepted
Miroslav
Telerik team
answered on 03 Aug 2009, 04:04 PM
Hi Frank,

Of course - in this example the icon of the Organisation template will be visible only if the numeric value is above 2.0. Hopefully you will be able to extend this approach for what you need. I am also attaching a very similar example where the items source of the item is replaced depending on a property. This extra example may not be relevant for your case but I am attaching it because it is very similar.

All the best,
Miroslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
James
Top achievements
Rank 1
answered on 03 Aug 2009, 08:45 PM
Hi Miroslav,

It took 10 minutes to implement and works perfectly.

Thanks for your help, James.
Tags
TreeView
Asked by
James
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
Frank
Top achievements
Rank 1
James
Top achievements
Rank 1
Share this question
or