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?
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>
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With