Silverlight Controls and Silverlight Toolkithttp://forums.silverlight.net//35.aspx/1?Silverlight+Controls+and+Silverlight+ToolkitDiscussions around using and developing Silverlight controls and the Silverlight ToolkitMon, 01 Jan 0001 00:00:00 -050035121511http://forums.silverlight.net//p/44457/121511.aspx/1?Toolkit+treeview+questionsToolkit treeview questions <p>Apologies if this seems lame, coming to this from a Win32/C&#43;&#43; background the terrain is a little unfamiliar (1). <br> </p> <p>I'd like to be able to use the Treeview and friends to display some multi-column data in a hierarchical fashion. Very similar to a threaded email display. So if I can do this with the new TreeView, how do I do it? (2)<br> </p> <p>Here's some guessing: </p> <p>1. Derive a new class from TreeViewItem, let's call it GridTreeNode. <br> </p> <p>2. Have a String[] or similiar to store content</p> <p>3. Add multiple GridTreeNode instances like this:</p> <p>_tree.Items.Add(new GridTreeNode(&quot;Root&quot;,&quot;11/11/1918&quot;)); </p> <p>4. At some point the item is going to get displayed - does it get asked for its content, or does it get told to display it?&nbsp;</p> <p>Finally is it possible to have a listview style header within the stock tree view?&nbsp;</p> <p>Thx&#43;&#43;, <br> </p> <p>Jerry <br> </p> <p><br> </p> <p>(1) - lots of scope for an informed article for the longer haried, shorter sighted API/MFC programmers out there. Where is WM_PAINT when I want it?</p> <p>(2) XAML examples are OK but programmatic examples would be better. A vastly superior choice would be to show the analogue of the XAML code in C# simplifying the task of creating a new runtime experience.<br> </p> <p>(3) - some of the TreeView naming is bizarre - one of the examples chez dwahlin has this:</p> <pre class="code"><span style="color:blue"> &lt;</span><span style="color:rgb(163,21,21)">controls</span><span style="color:blue">:</span><span style="color:rgb(163,21,21)">TreeViewItem </span><span style="color:red">Header</span><span style="color:blue">=&quot;ACME Corporation Employees&quot;&gt;</span></pre> <pre class="code">Now surely the property should be called Text? <br></pre> <p>&nbsp;</p> <p>&nbsp;</p> 2008-10-31T02:28:01-04:00121525http://forums.silverlight.net//p/44457/121525.aspx/1?Re+Toolkit+treeview+questionsRe: Toolkit treeview questions <p>Hi Jerry,</p> <p>&nbsp;Thanks for coping out about not being a native-WPFer. Now I need you to take one more step and admit that there's some learning curve in Silverlight/WPF before accomplishing more tasks. </p> <p>The main point you're missing is the Silverlight/WPF content model. The &quot;shouldn't Header be text?&quot; question is a wonderful example. WPF/Silverlight doesn't have very few places that are UI oriented with string-only display. That's because we like putting &quot;elements&quot; in other &quot;elements&quot;.</p> <p>So, Button doesn't have a &quot;Button.Text&quot; property, it has a &quot;Button.Content&quot; property so we could put a whatever in it, not just text.<br> Same goes for, &quot;Label.Content&quot; and &quot;TreeViewItem.Header&quot; and &quot;ComboBoxItem.Content&quot; and etc. </p> <p>So, the first thing you need to catch up on is - the content model. These resources might be a good start:<br> <a href="http://silverlight.net/learn/learnvideo.aspx?video=82688">http://silverlight.net/learn/learnvideo.aspx?video=82688</a><br> <a href="http://learnwpf.com/Posts/Post.aspx?postId=25343042-3ab8-4a36-b9f3-00433f4fd049">http://learnwpf.com/Posts/Post.aspx?postId=25343042-3ab8-4a36-b9f3-00433f4fd049</a><br> <a href="http://dotnet.org.za/rudi/archive/2008/04/07/why-wpf-rock-the-content-model.aspx">http://dotnet.org.za/rudi/archive/2008/04/07/why-wpf-rock-the-content-model.aspx</a></p> <p>Now, if you find those a bit diffecult to follow (and it's a lot to take in) you might want to spend some time brushing up on general silverlight skills at <a href="http://silverlight.net/GetStarted">http://silverlight.net/GetStarted</a>.</p> <p>Now, with the content model - there's no need to go around massivly subclassing stuff when changing UI, only when chaging behavior. </p> <p>Since all you need is changing UI for you custom whatever TreeView, you don't need to subclass anything.<br> And we'll use some SIlverlight DataBinding and Templating magic to achieve that. </p> <p>Let's start off from this simple CLR email class:</p> <div style="font-size:12pt; background:white; color:black; font-family:Courier New"> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue">public</span> <span style="color:blue"> class</span> <span style="color:#2b91af">Email</span></p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue">public</span> Email(<span style="color:blue">string</span> Title, <span style="color:#2b91af">DateTime</span> Date)</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue">this</span>.Title = Title;</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue">this</span>.Date = Date;</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue">public</span> <span style="color:blue"> string</span> Title { <span style="color:blue">get</span>; <span style="color:blue"> set</span>; }</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color:blue">public</span> <span style="color:#2b91af"> DateTime</span> Date { <span style="color:blue">get</span>; <span style="color:blue"> set</span>; }</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p> </div> <p>Next, we'll create a list of Emails.</p> <div style="font-size:12pt; background:white; color:black; font-family:Courier New"> <p style="margin:0px"><span style="color:blue">new</span> <span style="color:#2b91af"> List</span>&lt;Email&gt;()</p> <p style="margin:0px">{</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; <span style="color:blue">new</span> Email(<span style="color:#a31515">&quot;Hello Email&quot;</span>, <span style="color:#2b91af">DateTime</span>.Now),</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; <span style="color:blue">new</span> Email(<span style="color:#a31515">&quot;Hello World&quot;</span>, <span style="color:#2b91af">DateTime</span>.Now.AddDays(1)),</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; <span style="color:blue">new</span> Email(<span style="color:#a31515">&quot;foo&quot;</span>, <span style="color:#2b91af">DateTime</span>.Now.AddMinutes(30)),</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; <span style="color:blue">new</span> Email(<span style="color:#a31515">&quot;bar&quot;</span>, <span style="color:#2b91af">DateTime</span>.Now.AddYears(1)),</p> <p style="margin:0px">};</p> </div> <p><br> And place those inside our TreeView.ItemsSource (Some of the Silverlight item templating magic...)</p> <div style="font-size:12pt; background:white; color:black; font-family:Courier New"> <p style="margin:0px">trvEmails.ItemsSource = <span style="color:blue">new</span> <span style="color:#2b91af">List</span>&lt;Email&gt;()</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color:blue">new</span> Email(<span style="color:#a31515">&quot;Hello Email&quot;</span>, <span style="color:#2b91af">DateTime</span>.Now), </p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color:blue">new</span> Email(<span style="color:#a31515">&quot;Hello World&quot;</span>, <span style="color:#2b91af">DateTime</span>.Now.AddDays(1)), </p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color:blue">new</span> Email(<span style="color:#a31515">&quot;foo&quot;</span>, <span style="color:#2b91af">DateTime</span>.Now.AddMinutes(30)), </p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color:blue">new</span> Email(<span style="color:#a31515">&quot;bar&quot;</span>, <span style="color:#2b91af">DateTime</span>.Now.AddYears(1)), </p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; };</p> </div> <p>now, let's add our TreeView:</p> <div style="font-size:12pt; background:white; color:black; font-family:Courier New"> <p style="margin:0px"><span style="color:#a31515">&nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;</span><span style="color:#a31515">controls</span><span style="color:blue">:</span><span style="color:#a31515">TreeView</span><span style="color:red"> x</span><span style="color:blue">:</span><span style="color:red">Name</span><span style="color:blue">=&quot;trvEmails&quot;</span><span style="color:red"> Height</span><span style="color:blue">=&quot;150&quot;</span><span style="color:red"> Width</span><span style="color:blue">=&quot;142&quot;</span> </p> <p style="margin:0px">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <span style="color:red">ItemTemplate</span><span style="color:blue">=&quot;{</span><span style="color:#a31515">StaticResource</span><span style="color:red"> HelloWorldTemplate</span><span style="color:blue">}&quot;/&gt;</span><span style="color:#a31515"> </span></p> </div> <p>And lastly we'll add the ItemTemplate we'll generate:</p> <div style="font-size:12pt; background:white; color:black; font-family:Courier New"> <p style="margin:0px"><span style="color:#a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue">&lt;</span><span style="color:#a31515">UserControl.Resources</span><span style="color:blue">&gt;</span></span></p> <p style="margin:0px"><span style="color:#a31515"><span style="color:blue">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span><span style="color:blue">&lt;</span><span style="color:#a31515">DataTemplate</span><span style="color:red"> x</span><span style="color:blue">:</span><span style="color:red">Key</span><span style="color:blue">=&quot;HelloWorldTemplate&quot;&gt;</span></p> <p style="margin:0px"><span style="color:#a31515">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;</span><span style="color:#a31515">StackPanel</span><span style="color:red"> Orientation</span><span style="color:blue">=&quot;Vertical&quot;&gt;</span></p> <p style="margin:0px"><span style="color:#a31515">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;</span><span style="color:#a31515">controls</span><span style="color:blue">:</span><span style="color:#a31515">Label</span><span style="color:red"> Content</span><span style="color:blue">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:red"> Title</span><span style="color:blue">}&quot;/&gt;</span></p> <p style="margin:0px"><span style="color:#a31515">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;</span><span style="color:#a31515">controls</span><span style="color:blue">:</span><span style="color:#a31515">Label&nbsp;<span style="color:red">Foreground</span><span style="color:blue">=&quot;Gray&quot;</span></span><span style="color:red">&nbsp;Content</span><span style="color:blue">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:red"> Date</span><span style="color:blue">}&quot;/&gt;</span></p> <p style="margin:0px"><span style="color:#a31515">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;/</span><span style="color:#a31515">StackPanel</span><span style="color:blue">&gt;</span></p> <p style="margin:0px"><span style="color:#a31515">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;/</span><span style="color:#a31515">DataTemplate</span><span style="color:blue">&gt;</span></p> <p style="margin:0px"><span style="color:#a31515">&nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;/</span><span style="color:#a31515">UserControl.Resources</span><span style="color:blue">&gt;</span></p> </div> <p>&nbsp;Now, if we run this sample:</p> <p><img height="202" src="http://img02.picoodle.com/img/img02/3/10/30/f_TreeViewEmam_cca93ce.gif" width="264" style="width:264px; height:202px"></p> <p>&nbsp;Now, the next few step you'll have to take on your own. You'll need to read up on how Templating really works, how does the databinding mechanism works, and check out the&nbsp;HierarchicalDataTemplate TreeView uses. You can check that last part at our online samples in: <a href="http://silverlight.net/samples/sl2/toolkitcontrolsamples/run/default.html"> http://silverlight.net/samples/sl2/toolkitcontrolsamples/run/default.html</a>&nbsp;(TreeView --&gt; Basics --&gt; DataBinding, and you can see source if you click on the XAML file name on top) </p> <p>&nbsp;</p> <p>Best of luck, </p> <p>&nbsp;</p> 2008-10-31T03:30:30-04:00121838http://forums.silverlight.net//p/44457/121838.aspx/1?Re+Toolkit+treeview+questionsRe: Toolkit treeview questions <p>&nbsp;Hello Justin</p> <p>Thanks for the quick response. Silverlight is great, I've only got a problem with the shortage of documentation, the latter not unreasonable though given the pace of development.<br> </p> <p>OK I get the data binding bit - and I can now replicate this in code. What I don't get is how to replicate the programmable functionality of the Win32 tree control for example. In my case I want to have arbitrary levels of nesting - which is not something I can specify in the XAML data template. </p> <p>&nbsp;</p> <p>Question 1 then. Extending your Email class</p> <p>&nbsp;&nbsp;&nbsp; public class Email<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Email(string Title, DateTime Date)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Items = new List&lt;Email&gt;();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Title = Title;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Date = Date;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string Title { get; set; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public DateTime Date { get; set; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public List&lt;Email&gt; Items { get; set; }<br> &nbsp;&nbsp;&nbsp; }<br> </p> <p>In code I can now do this:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trvEmails.Items.Add(new Email(&quot;Email 1&quot;, DateTime.Now));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Email em2 = new Email(&quot;Email 2&quot;, DateTime.Now);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trvEmails.Items.Add(em2);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; em2.Items.Add(new Email(&quot;Email 2.1&quot;, DateTime.Now));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; em2.Items.Add(new Email(&quot;Email 2.2&quot;, DateTime.Now));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trvEmails.Items.Add(new Email(&quot;Email 3&quot;, DateTime.Now));</p> <p>but it is really unclear who manages the hierarchy. Is it the TreeView, the TreeViewItem or the class that is contained within the item.&nbsp; </p> <p>Question 2 is then this:</p> <p>If I have added em2 to the trvEmails, how do I find it in the tree?&nbsp; Use case: a new email arrives which is a response to email 3. I need to find it and add the reply as a child item. Do I need to maintain my own map/dictionary of Email instances or does the control do this for me?</p> <p>Your help much appreciated. <br> </p> <p>One quibble: <br> </p> <p>So, Button doesn't have a &quot;Button.Text&quot; property, it has a &quot;Button.Content&quot; property so we could put a whatever in it, not just text.<br> Same goes for, &quot;Label.Content&quot; and &quot;<b>TreeViewItem.Header</b>&quot; and &quot;ComboBoxItem.Content&quot; and etc. </p> <p>&nbsp;Can you spot the naming inconsistency here? No biggie.<br> </p> 2008-10-31T13:46:19-04:00121940http://forums.silverlight.net//p/44457/121940.aspx/1?Re+Re+Toolkit+treeview+questionsRe: Re: Toolkit treeview questions <p>you should use HierarchicalDataTemplate to get nesting</p> 2008-10-31T15:57:53-04:00121942http://forums.silverlight.net//p/44457/121942.aspx/1?Re+Re+Toolkit+treeview+questionsRe: Re: Toolkit treeview questions <p>Thanks but how do I achieve that in code? A really simple example is all I'm after. </p> <p>ATB</p> <p>Jerry</p> 2008-10-31T16:04:21-04:00122033http://forums.silverlight.net//p/44457/122033.aspx/1?Re+Re+Re+Toolkit+treeview+questionsRe: Re: Re: Toolkit treeview questions <p>&nbsp;I think the logical leap you may be missing, Jerry, is that you don't manually add the items to the treeview in code at all.<br> </p> <p>You specify in the HierarchicalDataTemplate what the source of the data is using the ItemsSource property, which should behind the scenes be an ObservableCollection of your type Email.&nbsp; The runtime will automatically create the items for you.</p> <p>The toolkit download does have good examples in it of what you are seeking, try the NestedHierarchicalDataTemplate example.&nbsp;&nbsp; <br> </p> 2008-10-31T18:13:41-04:00122046http://forums.silverlight.net//p/44457/122046.aspx/1?Re+Re+Re+Toolkit+treeview+questionsRe: Re: Re: Toolkit treeview questions <p>Hello Anye</p> <p>Thanks for the comments. I don't think it quite addresses the right issue however*</p> <p>Questions arising:</p> <p>&nbsp;1. How do you specify an potentially unbounded (but practically limited) hierarchy in a XAML data template? I cannot work that out from the Hierarchical sample which seems to me to have a very definite, hard-coded, nesting depth of 3 items. </p> <p>2. How is the hierarchy managed? Consider the ultra simple example of a set of nested folders. If I want to display the folder tree, which will change at runtime depending on the root item I specifiy, how do I display the hierachy.</p> <p>These are not hard questions - the use of XAML in the examples is cute but not comprehensive. XAML does not remove the need for code - yet!</p> <p>ATB</p> <p>Jerry</p> <p>* Adding items directly to the tree or to a collection is immaterial from a philosophical point of view - somewhere, ultimately, there must be a container</p> 2008-10-31T18:41:50-04:00122066http://forums.silverlight.net//p/44457/122066.aspx/1?Re+Re+Re+Re+Toolkit+treeview+questionsRe: Re: Re: Re: Toolkit treeview questions <p>Jerry,</p> <p>if you look at the sample at <a href="http://leeontech.wordpress.com/2008/10/30/changing-treeview-template/"> http://leeontech.wordpress.com/2008/10/30/changing-treeview-template/</a>, you will see the decleration of&nbsp; HierarchicalDataTemplate</p> <p>&lt;controls:HierarchicalDataTemplate x:Key=”dtTeam”&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TextBlock Text=”{Binding Name}” /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/controls:HierarchicalDataTemplate&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;controls:HierarchicalDataTemplate x:Key=”dtDivision”<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ItemsSource=”{Binding Path=Teams}”<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ItemTemplate=”{StaticResource dtTeam}”&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TextBlock Text=”{Binding Name}” /&gt;</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/controls:HierarchicalDataTemplate&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;controls:HierarchicalDataTemplate x:Key=”dtLeague”<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ItemsSource=”{Binding Divisions}”<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ItemTemplate=”{StaticResource dtDivision}”&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;TextBlock Text=”{Binding Name}” /&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/controls:HierarchicalDataTemplate&gt;</p> <p>&nbsp;In that sample, I had Hierarchy of Leagues-&gt;Divisions-&gt;Teams</p> <p>we can do this only to predetermined levels. In WPF we could specify the template to be applied based on datatype which can allow us to build a tree to nlevels(which are not known). I dont think we can do that in Silverlight unless I overlooked something</p> 2008-10-31T18:51:51-04:00122238http://forums.silverlight.net//p/44457/122238.aspx/1?Re+Toolkit+treeview+questionsRe: Toolkit treeview questions <p>Hi Jerry,</p> <p>&nbsp;I agree with Anye that there's a mental leap needed here to not jump straight back into code whenever you're a bit uncomfortable with XAML.</p> <p>In order to make the sample work with the modified email class, you need to make 2 changes. <br> 1. Instead of using an ItemTemplate use a HierarchicalItemTemplate.&nbsp;<br> 2. Specify an ItemsSource for the nested children of that templatex.</p> <p>&nbsp;<font color="#0000ff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue">&lt;</span></font><span style="color:#a31515">controls:HierarchicalDataTemplate</span><span style="color:red"> x</span><span style="color:blue">:</span><span style="color:red">Key</span><span style="color:blue">=&quot;HelloWorldTemplate&quot; <span style="color:blue"><font color="#ff0000">ItemsSource</font><span style="color:blue"><span style="color:blue">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:red"> Items</span><span style="color:blue">}&quot;/</span></span></span>&gt;<br> </span><span style="color:#a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;</span><span style="color:#a31515">StackPanel</span><span style="color:red"> Orientation</span><span style="color:blue">=&quot;Vertical&quot;&gt;<br> </span><span style="color:#a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;</span><span style="color:#a31515">controls</span><span style="color:blue">:</span><span style="color:#a31515">Label</span><span style="color:red"> Content</span><span style="color:blue">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:red"> Title</span><span style="color:blue">}&quot;/&gt;<br> </span><span style="color:#a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;</span><span style="color:#a31515">controls</span><span style="color:blue">:</span><span style="color:#a31515">Label&nbsp;<span style="color:red">Foreground</span><span style="color:blue">=&quot;Gray&quot;</span></span><span style="color:red">&nbsp;Content</span><span style="color:blue">=&quot;{</span><span style="color:#a31515">Binding</span><span style="color:red"> Date</span><span style="color:blue">}&quot;/&gt;<br> </span><span style="color:#a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;/</span><span style="color:#a31515">StackPanel</span><span style="color:blue">&gt;<br> </span><span style="color:#a31515">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">&lt;/</span><span style="color:#a31515"><span style="color:#a31515">controls:HierarchicalDataTemplate</span></span><span style="color:blue">&gt;</span></p> <p>The HierarchicalDataTemplate will call upon itself&nbsp;unless you specifiy an ItemsTemplate for other&nbsp;Templates of it's children.</p> <p>&nbsp;</p> <p>When a new email is received, you do not need to manually walk the tree and add it.&nbsp;If you use ObservableCollection&lt;Email&gt; to&nbsp;describe the list of&nbsp;nested items, the TreeView UI would be updated as soon as the&nbsp;underlying collection changes.&nbsp;&nbsp;<br> </p> <p>&nbsp;</p> <p>&nbsp;</p> 2008-10-31T23:56:19-04:00122266http://forums.silverlight.net//p/44457/122266.aspx/1?Re+Toolkit+treeview+questionsRe: Toolkit treeview questions <p>&nbsp;Hello Justin</p> <p>If you can do it in the mark-up then you should be able to do it in code. Not an unreasonable view I think. Practical use case? How about this:</p> <p>Change your view of email from threaded by reply (potentially unlimited nesting) to 'sorted by date'.&nbsp; </p> <p>In all of the examples posted so far you cannot do this kind of thing as the view of the data is firmly locked to the tree view control declaration in the XAML.</p> <p>Let me ask this another way. How would you code a control to mimic an Explorer style view of a file system using the TreeView control? </p> <p>Jerry<br> </p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;<br> </p> 2008-11-01T00:43:00-04:00122535http://forums.silverlight.net//p/44457/122535.aspx/1?Re+Toolkit+treeview+questionsRe: Toolkit treeview questions <p>&nbsp;OK - here is a programmatic solution to the problem. I hope this might save someone else 3 or 4 days of experimentation.</p> <p>1. The first thing is to set up a class to represent the data that is going to be viewed - the fact that this control is called a TreeView is, I think, no accident. It is quite different to a Win32/WinForms control. As my original question concerned email let's stick with that. Here is the class that we'll use. <br> </p> <p>&nbsp;&nbsp;&nbsp;&nbsp; // this will be displayed in the Silverlight TreeView<br> &nbsp;&nbsp;&nbsp; public class EmailItem<br> &nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // constructor takes various arguments as one might expect<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public EmailItem(string Title, string From,DateTime Date)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // by default a root item so no parent<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Parent = null;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the observable collection is very important here, it means that an instance of this class <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // will get notified if the collection changes,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // as well as the container getting notified of changes - see the Add() code later.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Items = new ObservableCollection&lt;EmailItem&gt;(); <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this is for sanity checking and general proof of understanding<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Items.CollectionChanged &#43;= <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new NotifyCollectionChangedEventHandler(Items_CollectionChanged);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // assign the constructor parameters&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Title = Title;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.From = From;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.Date = Date;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the collection has changed - just spew a debug message for the moment<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (sender != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.WriteLine(&quot;Items_CollectionChanged: &quot; &#43; sender.ToString() &#43; &quot;(&quot; &#43; e.Action.ToString() &#43; &quot;)&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // our email properties. <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string Title { get; set; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string From { get; set; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public DateTime Date { get; set; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public EmailItem Parent { get; set;}&nbsp; // the parent emailItem - enables us to work back up through the hierarchy<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this is special - this collection will contain all of the<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // emails which are children in the hierachy. Imagine them to be <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the collected replies to a specific email. The depth of the<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // hierarchy is bounded only by systems limits.<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public ObservableCollection&lt;EmailItem&gt; Items { get; set; }<br> &nbsp;&nbsp;&nbsp; }<br> </p> <p>2.&nbsp; Now we need to tell the tree view what is it going to display. The XAML here totallly dictates the appearance of the item in the view. This XAML snippet places a tree view on the layout and determines the content. Somewhere you will need to have added a namespace to the XAML referencing the Silverlight toolkit (SLTK)<br> </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xmlns:controls=&quot;clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls&quot; <br> </p> <p>So any SLTK controls are prefixed with a controls: namespace qualifier<br> </p> <p>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;controls:TreeView Margin=&quot;8,80,102,78&quot; x:Name=&quot;trvEmails&quot; SelectedItemChanged=&quot;ctlTree_SelectedItemChanged&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;controls:TreeView.ItemTemplate&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;controls:HierarchicalDataTemplate x:Name=&quot;EmailItemTemplate&quot; ItemsSource=&quot;{Binding Items}&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;controls:Label&nbsp; Width=&quot;80&quot; Content=&quot;{Binding Title}&quot;/&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;controls:Label&nbsp; Width=&quot;80&quot; Content=&quot;{Binding From}&quot;/&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;controls:Label Foreground=&quot;Gray&quot; Content=&quot;{Binding Date}&quot;/&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/StackPanel&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/controls:HierarchicalDataTemplate&gt;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/controls:TreeView.ItemTemplate&gt;<br> </p> <p>In English then, we've got a&nbsp; TreeView called trvEmails with an event handler called ctlTree_SelectedItemChanged that is invoked when the selection changes in the tree.</p> <p>Next comes the clever/tricky bit - the ItemTemplate. This contains a HierarchicalDataTemplate declaration which has as it source binding a property called Items. This is bound directly to the Items property in the EmailItem class. Importantly Items is not directly viewable - the bits of the email message we wish to display is specified by the StackPanel. Note I've chosen to orientate this horizontally - this means we get a columnar layout for the EmailItem with the content described by the 3 labels that follow. Note that each label is bound to a separate property of the EmailItem class - in turn Title, From and Date. Just to show we are in control, the date is drawn as greyed text.</p> <p>That, as they say, is that. Let's move on to the code.</p> <p>3. Now if you have added the C# code and the XAML markup to a project, the tree, at runtime, will be empty. Here's some code to populate the tree:</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; // I've added this code to the page constructor <br> </p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Page()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InitializeComponent();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add the first new email item to the root collection<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EmailItem emi = new EmailItem(&quot;Email 1&quot;,&quot;Root&quot;, DateTime.Now);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trvEmails.Items.Add(emi);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add a 'reply' to the first email<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emi.Items.Add(new EmailItem(&quot;Email 1.1&quot;, &quot;abc&quot;, DateTime.Now));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add another root item<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emi = new EmailItem(&quot;Email 2&quot;, &quot;Root&quot;, DateTime.Now);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // insert it into the view collection<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trvEmails.Items.Add(emi);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // and add a reply to the second item<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emi.Items.Add(new EmailItem(&quot;Email 2.1&quot;, &quot;abc&quot;,DateTime.Now));<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // finally a third root item<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trvEmails.Items.Add(new EmailItem(&quot;Email 3&quot;, &quot;def&quot;, DateTime.Now));<br> &nbsp; &nbsp; &nbsp; &nbsp; }</p> <p>This should be pretty self explanatory - what you get then is a tree that initially looks like this - note the arrows indicating that the item has children. Magic!<br> </p> <p><img src="file:///u:/temp/moz-screenshot-21.jpg" alt=""><img src="http://www.novadsp.com/sl2/treeview1.png" title="TrreView 1" alt="TrreView 1" height="222" width="371"></p> <p>and here is the tree with the items expanded:</p> <p><img src="http://www.novadsp.com/sl2/treeview2.png" title="Expanded Treeview" alt="Expanded Treeview" height="208" width="381">&nbsp;</p> <p>4.&nbsp; Neat. Let's code up the event handler that is called when the tree selection changes:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void ctlTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs&lt;object&gt; e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.WriteLine(&quot;ctlTree_SelectedItemChanged&quot;);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EmailItem oldItem = e.OldValue as EmailItem;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EmailItem newItem = e.NewValue as EmailItem;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // _selectedEmailItem is declared within the class as:<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // private EmailItem _selectedEmailItem;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _selectedEmailItem = newItem;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // see next paragraph<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; btnAdd.IsEnabled = (_selectedEmailItem != null);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (oldItem != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.WriteLine(&quot;Old: &quot; &#43; oldItem.Title);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (newItem != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EmailItem emi = newItem;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (emi != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.WriteLine(&quot;Walking back to root: &quot; &#43; emi.Title);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emi = emi.Parent;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> </p> <p>This handler updates an EmailItem instance (_selectedEmailItem) that is declared within the page code. This makes life a bit simpler in a moment,as I hope to show. Once we are sure we've updated the _selectedEmailItem we do a little bit more work to ensure we've actually tamed the beast and it is working as we expect. Caveat: I'm not sure this is 100% neccessary. There may be a more generic navigation mechanism built into the TreeViewItem but I've not found it yet. What we do when the selection changes is to walk back up the tree until the item under examination has a null parent - at each stage we print out the email title. </p> <p>5. Now, in the case of the items we have inserted so far, we have not set the Parent property. I've skipped over it entirely This might be useful if you want to refer back to the email in your reply for example. Assume a button called btnAdd has been added to the XAML and an suitable handler added to the C# code. Here's what the handler will do:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; private void btnAdd_Click(object sender, RoutedEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // invariant - cannot be null if button enabled<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (_selectedEmailItem != null)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // this new email is from jerry<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EmailItem emi = new EmailItem(&quot;re:&quot; &#43; _selectedEmailItem.Title,&quot;Jerry&quot;, DateTime.Now);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // and its parent is the current selection<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emi.Parent = _selectedEmailItem;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // add the new child to the collection in the parent<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _selectedEmailItem.Items.Add(emi);<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> </p> <p>This should be adequately commented - we add a new item to the tree as a child of the current selection. What is pretty clever is that at no point is the TreeView or any TreeViewItems involved. But, despite this absence of an explicit link, the action of adding the email will ensure the view is updated, with the modified item now displaying the 'i've got child items' arrow. See green callouts.<br> </p> <p><img src="http://www.novadsp.com/sl2/treeview3.png" title="Modified treeview" alt="Modified treeview" height="149" width="379">&nbsp;</p> <p>&nbsp;6. Finally. That covers the essentials and answers my initial question. Hope you find this useful. Please feel free to comment/query/criticise as you see fit. I'd appreciate it if the latter was constructive in nature!</p> <p>Jerry <br> </p> <p>&nbsp;</p> 2008-11-01T18:23:58-04:00124159http://forums.silverlight.net//p/44457/124159.aspx/1?Re+Toolkit+treeview+questionsRe: Toolkit treeview questions <p><font face="verdana,geneva">Hi Jerry,<br> <br> Unfortunately in Silverlight there are some things you can only do in XAML and not code.&nbsp; You can't currently create <font face="courier new,courier">DataTemplate</font>s (or <font face="courier new,courier"> HierarchicalDataTemplate</font>s, etc.) in code (except for using <font face="courier new,courier"> XamlReader.Load</font> on a string of XAML).<br> <br> You can still do most of your work in code if you prefer though.&nbsp; The trick is to create a regular <font face="courier new,courier">DataTemplate</font> that defines how you like your items rendered and stick it in the <font face="courier new,courier">Resources</font> of a named control.&nbsp; Then you can look it up with <font face="courier new,courier">DataTemplate emailTemplate = myControl.Resources[&quot;MyTemplate&quot;] as DataTemplate;</font>.&nbsp; Just create all your <font face="courier new,courier">TreeViewItem</font>s, set the <font face="courier new,courier"> Header</font> to be your <font face="courier new,courier">email</font>, and set the <font face="courier new,courier">HeaderTemplate</font> to be <font face="courier new,courier"> emailTemplate</font>.&nbsp; You can add children directly to the <font face="courier new,courier"> TreeViewItems.Items</font> collection.&nbsp; It's a little more work to set up, but you don't have to worry about whether items are in the visual tree when you construct them yourself.<br> </font><font face="verdana,geneva"><br> Thanks,<br> Ted</font></p> 2008-11-04T06:21:38-05:00124337http://forums.silverlight.net//p/44457/124337.aspx/1?Re+Toolkit+treeview+questionsRe: Toolkit treeview questions <p>Hello Ted,</p> <p>Thanks for the comment. May I suggest this gets added to the samples/documentation? Is it possible to knock up a code snippet that does this with an HDataTemplate?</p> <p>One thing - the problem with DataTemplates is that they violate the separation of design and code. All of a sudden I have to fill a not very easy to segment XAML document with a load of stuff that may or may not make sense to a Blend designer. A very unfortunate side effect is that you now have (effectively) duplicated class declarations, one in a C# file, the other in the XAML. It is, however, an intractable problem - after all you do have to tell the control what to display.</p> <p>While you are here: what is the best way to add a column header to a multi-column tree view like I have in my screen snaps?</p> <p>ATB</p> <p>Jerry</p> 2008-11-04T12:11:01-05:00124445http://forums.silverlight.net//p/44457/124445.aspx/1?Re+Re+Toolkit+treeview+questionsRe: Re: Toolkit treeview questions <p>&nbsp;This is a nice tutorial, Jerry.&nbsp; Unfortunately I'm still stumped in the scenario when you only want to load the top level items upfront - you can't see the expand-o button unless there is a child node, so there is a classic chicken-and-egg problem.</p> <p>&nbsp;Any ideas? <br> </p> 2008-11-04T14:49:20-05:00124476http://forums.silverlight.net//p/44457/124476.aspx/1?Re+Re+Toolkit+treeview+questionsRe: Re: Toolkit treeview questions <p>Anye - to clarify then the initial situation is that your tree has 3 root items. None of these has any children but you want to populate on demand? Here is one possible solution that assumes you've something similar to my EmailItem class. So:</p> <p>1. If its a root node then its parent is null - by definition. </p> <p>2. If nodes are to be populated on demand then the user is going to have to indicate which node is to be loaded and displayed. </p> <p>3. 2. implies you could use the selectionChanged event on the tree - so we have a couple of extra lines of code:</p> <p>EmailItem emi = newItem;<br> // has the item got any children? There are any number of variations on this theme - one could have a loaded flag in the item for example<br> if (emi.Items.Count == 0)<br> {<br> &nbsp;&nbsp;&nbsp; EmailItem ema = new EmailItem(&quot;re: &quot; &#43; emi.Title, &quot;1.0&quot;, DateTime.Now);<br> &nbsp;&nbsp;&nbsp; // this correctly sets up the visual state too.<br> &nbsp;&nbsp;&nbsp; emi.Items.Add(ema);<br> }<br> </p> <p>Does that help? I know it means the initial nodes will not display a &gt; <strong> until</strong> they are clicked but that is not too horrible to contemplate. You could flag this with textual or color changes.</p> <p>The alternative is to modify the code and add the ability to set the visual state to 'expandable' even if the collection of child items is empty. I hope that this is one of a number of improvements that will get added to this important control - trees have become essential elements in most of the apps I write these days. </p> <p>HTH &#43; ATB</p> <p>Jerry</p> 2008-11-04T15:30:30-05:00124530http://forums.silverlight.net//p/44457/124530.aspx/1?Re+Re+Re+Toolkit+treeview+questionsRe: Re: Re: Toolkit treeview questions <p>&nbsp;Thanks, Jerry.. after I posted it occured to me that I could probably fiddle with the TreeView source to make the button appear even if there is no child.&nbsp; I think I will try that.</p> <p>The reason I didn't want to just use the selectedChanged is that my TreeViewItem templates have either checkboxes or radio buttons depending on where they are being used - so I don't necessarily want them &quot;selected&quot; when expanding :)</p> <p>&nbsp;</p> <p>Cheers,</p> <p>Anye <br> </p> 2008-11-04T16:06:42-05:00124534http://forums.silverlight.net//p/44457/124534.aspx/1?Re+Re+Re+Toolkit+treeview+questionsRe: Re: Re: Toolkit treeview questions <p>Anye, maybe it is really easy to set the right visual state. Is not all this stuff in the XAML template? </p> 2008-11-04T16:13:59-05:00125272http://forums.silverlight.net//p/44457/125272.aspx/1?Re+Re+Re+Toolkit+treeview+questionsRe: Re: Re: Toolkit treeview questions <p>Another option would be to Load the top level of the Hierarchy up front and then load the data needed to expand each of the top level nodes asynchrounoulsly in the background. This way once the backgound task to get the data for&nbsp;a given node&nbsp;has completed&nbsp; and updated the ObservableCollection for&nbsp;that node &nbsp;to have children, the ui would automatically display the Expand-o button. </p> <p>If you have only a few top-level nodes with just one level of nested nodes this may be useful. You show the user the top level nodes quickly, and as the data is retrived for each of thos nodes the Expander button appears.&nbsp; </p> 2008-11-05T16:41:40-05:00125412http://forums.silverlight.net//p/44457/125412.aspx/1?Re+Re+Re+Toolkit+treeview+questionsRe: Re: Re: Toolkit treeview questions <p>&nbsp;Hello Russ, interesting idea - have you tried this? <br> </p> 2008-11-05T20:17:07-05:00125558http://forums.silverlight.net//p/44457/125558.aspx/1?Re+Re+Re+Toolkit+treeview+questionsRe: Re: Re: Toolkit treeview questions <p>Yes, I have.&nbsp; The top level nodes display once the control is loaded, and then the 'expand-o' images start to appear one ate a time on each top level node.</p> <p>I'm calling a WCF service to fill my top level. nodes. When I get the results from that call I'm looping through the results adding nodes to my ObservableCollection and also -- for each node-- calling another service to get the nodes children. </p> <p>Not sure if this is the right way to do this, but it looks really cool watching the expand-o icons light up one after another going down the page :-}</p> <p>&nbsp;</p> <p>&nbsp;</p> 2008-11-06T01:59:50-05:00