Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand whole TreeView in Silverlight

How can I expand the whole TreeView in Silverlight?

EDIT: Here is the XAML :

<controls:TreeView x:Name="tv">
    <controls:TreeView.ItemTemplate>
        <common:HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <CheckBox IsChecked="{Binding Visible, Mode=TwoWay}" Content="{Binding Name}"/>
        </common:HierarchicalDataTemplate>
    </controls:TreeView.ItemTemplate>
</controls:TreeView>

Perhaps using the ItemTemplate makes the ItemContainerGenerator.ContainerFromIndex return null on any index?

like image 362
Andrei Rînea Avatar asked Sep 02 '09 08:09

Andrei Rînea


2 Answers

I know this is a bit late but I found this when looking for an answer and have since found that in Silverlight 4 you can use implicit theming to do this:

<Style TargetType="controls:TreeViewItem">
    <Setter Property="IsExpanded" Value="True" />
</Style>

In Silverlight 5 you will have to add this code to the section to achieve this.

<Style TargetType="sdk:TreeViewItem">
    <Setter Property="IsExpanded" Value="True" />
</Style>
like image 178
Mark Avatar answered Oct 20 '22 16:10

Mark


First, read this post: http://bea.stollnitz.com/blog/?p=55

Second, inherit TreeViewItem and TreeView:

public class TreeViewItemEx : TreeViewItem {
    protected override DependencyObject GetContainerForItemOverride() {
        TreeViewItemEx tvi = new TreeViewItemEx();
        Binding expandedBinding = new Binding("IsExpanded");
        expandedBinding.Mode = BindingMode.TwoWay;
        tvi.SetBinding(TreeViewItemEx.IsExpandedProperty, expandedBinding);
        return tvi;
    }
}

public class TreeViewEx : TreeView {
    protected override DependencyObject GetContainerForItemOverride() {
        TreeViewItemEx tvi = new TreeViewItemEx();
        Binding expandedBinding = new Binding("IsExpanded");
        expandedBinding.Mode = BindingMode.TwoWay;
        tvi.SetBinding(TreeViewItemEx.IsExpandedProperty, expandedBinding);

        return tvi;
    }
}

Third, binding your Model's property to "IsExpanded".

like image 31
Homer Avatar answered Oct 20 '22 15:10

Homer