tags:

views:

1093

answers:

3

I've created a (System.Windows.Controls.Primitives.)Popup that contains a Treeview that I'm populating with data read in from a file. The Popup is created in code rather than in xaml. I want to add checkboxes to the treeview in a similar way to the CheckedTreeViewItemSample.xaml that comes in the Silverlight Toolkit.

I'm having trouble converting the xaml into code because I don't really understand how the HierarchicalDataTemplate works and how the bindings work. In fact I find the Silverlight object model pretty confusing!

I've done quite a bit of searching but I haven't found how to do it in code-behind. Will I need to define the xaml and use the XamlReader?

Thanks in advance...

+1  A: 

You need to put the controls into the header property of the treeviewitems you're adding:

        var myTreeViewItem = new TreeViewItem();
        var myContentHolder = new StackPanel();
        var myCheckBox = new CheckBox();
        TextBlock myTextBlock = new TextBlock();
        myTextBlock.Text = "blah";
        //add more controls
        myContentHolder.Children.Add(myCheckBox);
        myContentHolder.Children.Add(myTextBlock);
        myTreeViewItem.Header = myContentHolder;

Voila. To be honest, you do this the first time you use Silverlight, then try doing it with xaml and never go back to doing it in code. Also it's much easier if you wrap the control you're going to use in the header as a usercontrol. And then you can make that custom control into another file (xaml-based, of course ;)

mattmanser
Ah... got it! Thanks for the suggestion too. It seems so clear once the blindingly obvious is pointed out :o)
fran
A: 

This throws an error on my pc. The only thing i can put in the header without an exception from the codebehind is text.

(i believe its related to this http://silverlight.codeplex.com/WorkItem/View.aspx?WorkItemId=2597)