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

Setting "IsExpanded" on Templated Node

4 Answers 117 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Phil Cockfield
Top achievements
Rank 1
Phil Cockfield asked on 25 Sep 2009, 12:21 AM
I'm using the 'ItemTemplate' for the RadTreeView control like this:

    <UserControl.Resources> 
        <!--Tree Node--> 
        <tcore:HierarchicalDataTemplate x:Key="node"
            <StackPanel Orientation="Horizontal"
                <Image Source="{Binding Icon}" Margin=" 0,0,6,0" /> 
                <TextBlock Text="{Binding Text}"  /> 
            </StackPanel> 
        </tcore:HierarchicalDataTemplate> 
    </UserControl.Resources> 
     
    <!--TreeView--> 
    <t:RadTreeView  
                ItemsSource="{Binding Nodes}" 
                ItemTemplate="{StaticResource node}"  
                IsLineEnabled="True" /> 
 

I'm wondering, within my "node" template, how can I get set whether the node IsExpanded of not.  Like the way you could do it if you were using:

            <t:RadTreeViewItem  
                        DefaultImageSrc="{Binding Icon}" 
                        Header="{Binding Text}" 
                        IsExpanded="True" ItemsSource="{Binding Children}" /> 
 

Do I have to wrap my templated XAML in something.  Putting a RadTreeViewItem within the template caused the control to crash, so I'm not sure what the right way is to approach this.

Thanks for your help!


4 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 25 Sep 2009, 07:11 AM
Hi Phil Cockfield,

The ItemTemplate property is used for templating how the Header of the item will look like.

The IsExpanded property should be set via style. So you will need to define the ItemContainerStyle property of the RadTreeView and set the IsExpanded to True there. Here is a sample:

// declare the style

 

 

<UserControl.Resources>

 

 

 

<Style TargetType="telerikNavigation:RadTreeViewItem" x:Key="TreeItemTemplate">

 

 

 

<Setter Property="IsExpanded" Value="True"/>

 

 

 

</Style>

 

 

 

</UserControl.Resources>

 


// set the style to the TreeView

<

 

telerikNavigation:RadTreeView ItemContainerStyle="{StaticResource TreeItemTemplate}"

 


Kind regards,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Phil Cockfield
Top achievements
Rank 1
answered on 26 Sep 2009, 07:29 PM
Thanks Valentin.  I just tried that and it didn't work.  See code:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:local="clr-namespace:Quest.Core.UI.Controls"  
    xmlns:t="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"  
    xmlns:tcore="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls"
 
    <ControlTemplate x:Key="TreeView" TargetType="local:TreeView"
        <Border> 
            <Border.Resources> 
                <!--Tree Node--> 
                <tcore:HierarchicalDataTemplate x:Key="node" ItemsSource="{Binding Children}"
                    <StackPanel Orientation="Horizontal"
                        <Image Source="{Binding Icon}" Margin=" 0,0,6,0" /> 
                        <TextBlock Text="{Binding Text}"  /> 
                    </StackPanel> 
                </tcore:HierarchicalDataTemplate> 
 
                <Style x:Key="TreeItemTemplate" TargetType="t:RadTreeViewItem"
                    <Setter Property="IsExpanded" Value="True"/> 
                </Style> 
 
            </Border.Resources> 
 
            <!--TreeView--> 
            <t:RadTreeView  
                ItemsSource="{Binding Nodes}" 
                IsLineEnabled="True" 
                 
                ItemTemplate="{StaticResource node}"  
                ItemContainerStyle="{StaticResource TreeItemTemplate}" 
                 
                Padding="{TemplateBinding Padding}" 
                Background="{TemplateBinding Background}"  
                BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}" /> 
        </Border> 
    </ControlTemplate> 
</ResourceDictionary> 

What am I doing wrong here?

If it did work, I can't see how it gets to the solution I need which is to be able to control the 'IsExpanded' state (open/close) via data-binding from the model. I would envision the above code forcing all nodes to be open (IsExpanded=true) because the value is set at the root TreeView level.

Thanks for any more answers you might have!

0
Phil Cockfield
Top achievements
Rank 1
answered on 26 Sep 2009, 09:21 PM
Opps - sorry, yes that is causing them all to be expanded.

However my secondary question below still stands.  How can I structure this so that I can set the 'IsExpanded' value for each node via data-binding to the model bound to the Node's data-context?

Thanks!
0
Valentin.Stoychev
Telerik team
answered on 27 Sep 2009, 05:11 PM
Hi Phil Cockfield,

To achieve this you need to use our inhouse developed container bindings:
1. Here is how to define the bindings to your bussines object:

 

 

<telerik:ContainerBindingCollection x:Key="containerBindings">

 

 

 

<telerik:ContainerBinding PropertyName="Header" Binding="{Binding Caption}" />

 

 

 

<telerik:ContainerBinding PropertyName="IsExpanded" Binding="{Binding Expanded}" />

 

 

 

</telerik:ContainerBindingCollection>

 

2. Declare the bindings for the data template:

<

 

DataTemplate x:Key="MyTemplate" telerik:ContainerBinding.ContainerBindings="{StaticResource containerBindings}" >

 

 

 

<TextBlock Text="{Binding}"/>

 

 

 

</DataTemplate>

 



Then ofcouse set the DataTemplate as an ItemTemplate. What the container bindings will do is they will bind a property from the RadTreeViewItem ( the container) to a property of your bussinse object. In our case it will bind the Header property of the RadTreeViewITem to the Caption property of the bussines object.

Please let us know how it goes and if you have any other questions.


Sincerely yours,
Valentin.Stoychev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
TreeView
Asked by
Phil Cockfield
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Phil Cockfield
Top achievements
Rank 1
Share this question
or