Contingent ItemTemplateSelector

3 posts, 1 answers
  1. James
    James avatar
    119 posts
    Member since:
    Mar 2009

    Posted 20 May 2010 Link to this post


    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;
                    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. Answer
    Miroslav avatar
    922 posts

    Posted 26 May 2010 Link to this post

    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,
    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.
  3. James
    James avatar
    119 posts
    Member since:
    Mar 2009

    Posted 26 May 2010 Link to this post

    Hi Miroslav,

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

Back to Top