none
Checkbox treeview example from toolkit

    Question

  • Hi

     I have stripped the example from the toolkit. Made a new class Node that inherrit from Future, so there is a value IsExpanded in all objects in the treeview. This value is shown as tooltip now. In the example the IsExpanded is set to true for all treeviewitems. I'd like to use my IsExpanded value to show the tree like this at startup. Just ignore the selection.

     

    Here you can download my example code.

     

    Any suggestions

     

    Olav Haga

    Saturday, November 15, 2008 1:10 AM

Answers

  • Silverlight does not support binding in Styles.

    There isn't any way I can think of that would let you bind directly to the generated ItemContainers (TreeViewItems).

    I would just do that in code.  

                trv.Loaded +=new RoutedEventHandler(trv_Loaded);

           

     

            void trv_Loaded(object sender, RoutedEventArgs e)

            {

                foreach (myItem bussinessObject in trv.Items)

                {

                    TreeViewItem treeViewItem = (TreeViewItem) trv.ItemContainerGenerator.ContainerFromItem(bussinessObject);

                    treeViewItem.IsExpanded = myItem.IsExpanded;

                }

            }

    I can't think of anything we could do to make this scenario simpler (maybe implement ItemContainerGenerator.ItemChanging event), but feel free to open up a codeplex issue if you feel it's needed.
    http://www.codeplex.com/Silverlight/WorkItem/Create.aspx 

    Monday, November 24, 2008 5:24 PM

All replies

  • Hi Olav,

    Thanks for sharing your code with us.   I got a suggestion.  When the selected node has children and user press Enter key, it needs to be expanded. Also, IsExpanded is not a suitable property name, which may cause the confusion.   Any way, your sample really shed us light on the TreeView customization.    Thanks.

    Best regards,

    Jonathan

     

    Friday, November 21, 2008 8:03 AM
  • Hi Olav,

    Unfortunately I have no idea what it is you're saying, have done or trying to do. If you can put a bit more effort next time to correct grammar that would be excellent.

    From what I understand you have a TreeView with a CheckBox in the default ItemTemplate and you want to mark all of them by default.
    If i remember the sample correctly, marking the top TreeViewITem should be enough for that.

    If you were asking anything else, please attach a minimal and relevant code sample next time.

    Friday, November 21, 2008 2:22 PM
  • Hi Justin.

     

    I'll try to explain once more. In my example all the treeviewnodes is expanded.

    Beacuse of this

    <controls:TreeView.ItemContainerStyle>

    <Style TargetType="controls:TreeViewItem">

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

    </Style>

    </controls:TreeView.ItemContainerStyle>

     

    Id like to just expand some nodes. That's why I have added a IsExpanded property to my node objects.

     

    In my case I want the tree to load as in the picture. Level 1 expanded and the rest not expanded

     

    My first guess was something like this, but that just get a xmlParseException 

    <controls:TreeView x:Name="tv" ItemTemplate="{StaticResource NodeTemplate}">

    <controls:TreeView.ItemContainerStyle>

    <Style TargetType="controls:TreeViewItem">

    <Setter Property="IsExpanded" Value="{Binding IsExpanded}"/>

    </Style>

    </controls:TreeView.ItemContainerStyle>

    </controls:TreeView>

     

    Olav
     

     

    Monday, November 24, 2008 1:03 AM
  • Silverlight does not support binding in Styles.

    There isn't any way I can think of that would let you bind directly to the generated ItemContainers (TreeViewItems).

    I would just do that in code.  

                trv.Loaded +=new RoutedEventHandler(trv_Loaded);

           

     

            void trv_Loaded(object sender, RoutedEventArgs e)

            {

                foreach (myItem bussinessObject in trv.Items)

                {

                    TreeViewItem treeViewItem = (TreeViewItem) trv.ItemContainerGenerator.ContainerFromItem(bussinessObject);

                    treeViewItem.IsExpanded = myItem.IsExpanded;

                }

            }

    I can't think of anything we could do to make this scenario simpler (maybe implement ItemContainerGenerator.ItemChanging event), but feel free to open up a codeplex issue if you feel it's needed.
    http://www.codeplex.com/Silverlight/WorkItem/Create.aspx 

    Monday, November 24, 2008 5:24 PM
  • Thanks. Just what I needed :)

     

    Olav

    Tuesday, November 25, 2008 12:53 AM
  • Hi Olav and Justin,

    Sorry for the misunderstanding.   I have tested it on the sample provided by Olva.  We cannot get the treeViewItem since it is using ItemTemplate.  It is a lifecycle problem.   I finally added it to the StackPanel.   For example,

     

            <StackPanel.Resources>
    <controls:HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding Subcomponents}">
    <StackPanel Orientation="Horizontal" Loaded="StackPanel_Loaded">
    <CheckBox
    IsTabStop="False"
    IsThreeState="{Binding HasSubcomponents}"
    IsChecked="{Binding ShouldInstall, Mode=TwoWay}"
    Click="ItemCheckbox_Click"
    ToolTipService.ToolTip="{Binding IsExpanded}"
    Content="{Binding FeatureName}"
    />
    </StackPanel>
    </controls:HierarchicalDataTemplate>
    </StackPanel.Resources>

            private void StackPanel_Loaded(object sender, RoutedEventArgs e)
            {
                StackPanel sp = sender as StackPanel;
                TreeViewItem treeViewItem = (TreeViewItem)tv.ItemContainerGenerator.ContainerFromItem(sp.DataContext);

                if (treeViewItem != null)
                    treeViewItem.IsExpanded = false;
            } 

    Best regards,

    Jonathan

    Tuesday, November 25, 2008 2:27 AM
  • Jonathan, I got the same problem as you got on the sample. But in my application it works fine. I use Feature objects to build the tree.

    <control:HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding Subcomponents}">

    <StackPanel Height="16" Orientation="Horizontal">

    <CheckBox Height="16" FontSize="10" IsTabStop="False" Content="{Binding FeatureName}" IsThreeState="{Binding HasSubcomponents}" IsChecked="{Binding ShouldInstall, Mode=TwoWay}" Click="ItemCheckbox_Click" Margin="0" />

    </StackPanel>

    </control:HierarchicalDataTemplate>

     <control:TreeView x:Name="TreeViewUnit" ItemTemplate="{StaticResource NodeTemplate}" />

    In code this is added:

    TreeViewUnit.Loaded += new RoutedEventHandler(TreeViewUnit_Loaded);

    //add List<Feature>

    TreeViewUnit.ItemsSource = treeViewData.Items;

     

     

    private void TreeViewUnit_Loaded(object sender, RoutedEventArgs e)

    {

    foreach (Feature bussinessObject in TreeViewUnit.Items)

    {

    TreeViewItem treeViewItem = (TreeViewItem)TreeViewUnit.ItemContainerGenerator.ContainerFromItem(bussinessObject);

    if (treeViewItem != null)

    treeViewItem.IsExpanded = true;

    }

    }

     

    This expand all TreeViewItems on level 1. And that is what my customer wants.

    Olav

    Tuesday, November 25, 2008 2:54 AM
  • Hi Olav,

    Thanks!  Stick out tongue

    Best regards,

    Jonathan 

     

    Tuesday, November 25, 2008 11:21 PM
  • There is one bug. Sometimes the treeViewItem is null, and the bussinessObject is not.

    This was resolved by running the code in a DispatcherTimer that tick after 1 ms. Now the treeViewItem is never null

    Friday, November 28, 2008 3:52 AM
  • Hi Olav,

    can you please repro this bug in a new project and supply us with a minimal and relevant code sample?

    Registering to the TreeView.Loaded event should have all items loaded in the ItemContainerGenerator.

     

    Saturday, November 29, 2008 6:18 PM
  • The problem is in this line

     TreeViewItem treeViewItem = (TreeViewItem)tv.ItemContainerGenerator.ContainerFromItem(bussinessObject);

    The bussinessObject is a feature object, but the treeViewItem is sometimes null.

     I can upload a sample later today.

    Monday, December 01, 2008 1:18 AM
  • Here is the sample showing the bug

     

     

    Tuesday, December 02, 2008 12:42 AM
  • hi Justin,  I'm trying to create a treeview with checkboxes even in tri-state. my problem is that the controls:HierarchicalDataTemplate is not recognized in my Page.xaml. I'm using silverlight 2 and I have also installed the silverlight 2 toolkit. i have also added the system.windows.controls in my project (References).

    I'm planning to insert this HierarchicalDataTemplate inside the tabitem.

    <my2:TabItem>

    </my2:TabItem>

    I have also this piece of code:

    <UserControl xmlns:my2="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  xmlns:my1="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"  xmlns:my="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"    x:Class="MediaExchangeMyMedia3.Page"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="980" Height="476">

     

    hope you can help me...

     

    Thanks in advance...

     

    Tuesday, June 09, 2009 5:40 AM
  •  

    try this http://usamawahabkhan.blogspot.com/2009/07/treeview-checkbox-databinding.html
    Tuesday, July 28, 2009 3:58 AM
  • thanks it is really helpful

    Friday, June 08, 2012 4:05 AM