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

Contingent ItemTemplateSelector

2 Answers 57 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 21 May 2010, 12:19 AM
Hi,

I want to determine the selection of ItemTemplateSelector depending on a property of my user control.

I have a property:

private static string _showItemEditOptions;

        public string ShowItemEditOptions
        {
            get { return _showItemEditOptions; }
            set { _showItemEditOptions = value; }
        }

That is set in xaml:

<local:CodesTreeControl x:Name="codesTreeControl" ShowItemEditOptions="False"></local:CodesTreeControl>

I then want to determine the ItemTemplateSelector I use based on the value of this control:

if (ShowItemEditOptions == "False")
            {
                TreeView.ItemTemplateSelector = this.Resources["myReadOnlyTemplateSelector"] as ReadOnlyTemplateSelector;
            }
            else
            {
                TreeView.ItemTemplateSelector = this.Resources["myAttributeCodingTemplateSelector"] as AttributeCodingTemplateSelector;
            }

So far so good. I can change the ItemTemplateSelector based on the value of a variable. Unfortunately, I can't access my property immediately after InitializeComponent(); as it hasn't had time to load. If I leave it any later than this, then the treeview has intialised itself and it's too late to set the ItemTemplateSelector.

Is there a TreeView event I can access to set the ItemTemplateSelector in time?

Thanks, James.



2 Answers, 1 is accepted

Sort by
0
Accepted
Miroslav
Telerik team
answered on 26 May 2010, 01:48 PM
Hello James,

I am sorry for the delayed reply.

You can put your ItemTemplateSelector in the UserControl's resources and retrieve ti from the user control's code with:

var selector = this.Resource["mySelectorKey"] as MySelectorClass;

Then you can update the selector when the property is changed in your user control.

Likewise you can connect the user control and the selector after the Initialize() method of the user control.

If you want to change the ShowItemEditOptions dynamically I can suggest the following:

- Use just one ItemTemplate (of course you can keep the selector if you have different item types)
- Include the ItemEditOptions in this data template
- Create an observable class, say DisplayOptions that has an observable property EditOptionsVisibility of type visibility.
- Put this class in the UserControl's Resources, say with a key "displayOptions".
- Databind the visibility of the portion of the contents that you want to be different in the template to this property, by using binding Source

<Grid Visibility="{Binding EditOptionsVisibility, Source={StaticResource displayOptions}}" > ... </Grid>

This way the visibility (or any other property) of a portion of the template will be controlled from a single place.

If you keep all your options in an observable object you can create a wrapper object that retrieves them statically from somewhere so that they will be synced and used appropriately.

Sincerely yours,
Miroslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
James
Top achievements
Rank 1
answered on 26 May 2010, 04:14 PM
Hi Miroslav,

Thanks - that's really helpful. Lots of options to consider there!

James.
Tags
TreeView
Asked by
James
Top achievements
Rank 1
Answers by
Miroslav
Telerik team
James
Top achievements
Rank 1
Share this question
or