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 -050035227974http://forums.silverlight.net//p/99991/227974.aspx/1?Adding+Treeview+Items+dynamically+in+codebehind+error+as+Invalid+XamlAdding Treeview Items dynamically in codebehind! error as Invalid Xaml <p>Hi All,</p> <p>&nbsp;</p> <p>Iam Trying to build a Treeview dynamically, iam able to add treeviewitems dynamically, but after one level of hierarchy i would like to add a Grid control to the TreeviewItem. The grid contains a textblock,combobox and a button.&nbsp; Not able to add a Grid Object: error an invalid XAML.</p> <p>How do i solve it: and if any one can give an example would be very useful.</p> <p>Following is the code.</p> <font color="#2b91af" size="2"><font color="#2b91af" size="2">Grid</font></font><font size="2"> gd = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Grid</font></font><font size="2">();</font><font size="2"> <p></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">RowDefinition</font></font><font size="2"> rd1 = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">RowDefinition</font></font><font size="2">();</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ColumnDefinition</font></font><font size="2"> cd1 = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ColumnDefinition</font></font><font size="2">();</font><font size="2"> <p></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ColumnDefinition</font></font><font size="2"> cd2 = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ColumnDefinition</font></font><font size="2">();</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ColumnDefinition</font></font><font size="2"> cd3 = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ColumnDefinition</font></font><font size="2">();</font><font size="2"> <p>gd.RowDefinitions.Add(rd1);</p> <p>gd.ColumnDefinitions.Add(cd1);</p> <p>gd.ColumnDefinitions.Add(cd2);</p> <p>gd.ColumnDefinitions.Add(cd3);</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">TextBlock</font></font><font size="2"> tb = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">TextBlock</font></font><font size="2">();</font><font size="2"> <p>tb.Text = </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;Skills List&quot;</font></font><font size="2">;</p> tb.VerticalAlignment = </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">VerticalAlignment</font></font><font size="2">.Top;</font><font size="2">tb.HorizontalAlignment = </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">HorizontalAlignment</font></font><font size="2">.Left;</font><font size="2"> <p>&nbsp;</p> <p></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ComboBox</font></font><font size="2"> cmb = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ComboBox</font></font><font size="2">();</p> cmb.Name = </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;cmb&quot;</font></font><font size="2"> &#43; ti.Header.ToString();</font><font size="2"> <p>cmb.ItemsSource = l;</p> cmb.VerticalAlignment = </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">VerticalAlignment</font></font><font size="2">.Top;</font><font size="2"> <p>cmb.HorizontalAlignment = </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">HorizontalAlignment</font></font><font size="2">.Center;</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Button</font></font><font size="2"> btn = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Button</font></font><font size="2">();</font><font size="2"> <p>btn.Name = </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;btn&quot;</font></font><font size="2"> &#43; ti.Header.ToString();</p> btn.Click &#43;= </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">RoutedEventHandler</font></font><font size="2">(btn_Click);</font><font size="2"> <p>btn.Content = </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;Add to my Skillset&quot;</font></font><font size="2">;</p> btn.VerticalAlignment = </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">VerticalAlignment</font></font><font size="2">.Top;</font><font size="2"> <p>btn.HorizontalAlignment = </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">HorizontalAlignment</font></font><font size="2">.Right;</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Grid</font></font><font size="2">.SetRow(tb, 0);</font><font size="2"> <p></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Grid</font></font><font size="2">.SetColumn(tb, 0);</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Grid</font></font><font size="2">.SetRow(cmb, 0);</font><font size="2"> <p></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Grid</font></font><font size="2">.SetColumn(cmb, 1);</p> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Grid</font></font><font size="2">.SetRow(btn, 0);</font><font size="2"></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">Grid</font></font><font size="2">.SetColumn(btn, 2);</font><font size="2"> <p>gd.Children.Add(tb);</p> <p>gd.Children.Add(cmb);</p> <p>gd.Children.Add(btn);</p> <font size="2"> <p>//Treeviewitem object&nbsp;</p> <p>ti.Items.Add(gd);</p> </font></font> 2009-06-02T12:24:48-04:00228209http://forums.silverlight.net//p/99991/228209.aspx/1?Re+Adding+Treeview+Items+dynamically+in+codebehind+error+as+Invalid+XamlRe: Adding Treeview Items dynamically in codebehind! error as Invalid Xaml <p>Normally, you bind a collection [which has the hierarchy you want] to the TreeView itemsSource property And set the itemTemplate for the treeView to a HierarchicalDataTemplate [which also corresponds to the way you want to display your hierarchy]. For example check out the silverlight toolkit sample for SL2 at following link</p> <p><a href="http://silverlight.net/samples/sl2/toolkitcontrolsamples/run/default.html">http://silverlight.net/samples/sl2/toolkitcontrolsamples/run/default.html</a></p> <p>Look for samples of Treeview and NestedHierarchicalTemplate.</p> <p>you will get a good idea of what i am talking about.</p> <p>Hope this was helpful</p> <p>Ravi</p> 2009-06-03T00:19:13-04:00228427http://forums.silverlight.net//p/99991/228427.aspx/1?Re+Adding+Treeview+Items+dynamically+in+codebehind+error+as+Invalid+XamlRe: Adding Treeview Items dynamically in codebehind! error as Invalid Xaml <p>Thanks for the reply..but didnt solve my problem as there is no fixed hierarchy throught my tree view..........anyways i got the solution.......im applying a Controltemplate to the treeviewitem.template..........to show the grid........iam build a xaml string of my controltemplate with grid in it and using xamlreader.load() and then applying to Treeview.Template........the grid is shown...but iam not able to add a &quot;Click&quot; attribute for the button in my xaml as it shows xaml parser exception.......i have gone through lots of post and its said we cant do that......How do i assign a click event handler to my button???...even tried with XAMLwriter by building an object and then coming out with xaml......... Or is there any other way other than xamlreader.load().......to apply the controltemplate with my grid in it at runtime to a treeviewitem.template????</p> <p>Following is the code</p> <font size="2"> <p></font><font color="#2b91af" size="2"><font color="#2b91af" size="2">StringBuilder</font></font><font size="2"> tempString = </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">new</font></font><font size="2"> </font><font color="#2b91af" size="2"><font color="#2b91af" size="2">StringBuilder</font></font><font size="2">();</font></p> <font color="#0000ff" size="2"><font color="#0000ff" size="2">string</font></font><font size="2"> ns = </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;xmlns=\&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation\&quot;&quot;</font></font><font size="2">;</font><font size="2"> <p>tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;ControlTemplate &quot;</font></font><font size="2"> &#43; ns &#43; </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&gt;&quot;</font></font><font size="2">);</p> tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;Grid&gt;&quot;</font></font><font size="2">);</font><font size="2"> <p>tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;Grid.ColumnDefinitions&gt;&quot;</font></font><font size="2">);</p> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">for</font></font><font size="2"> (</font><font color="#0000ff" size="2"><font color="#0000ff" size="2">int</font></font><font size="2"> i = 0; i &lt; 3; i&#43;&#43;)</font><font size="2"> <p>{</p> tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;ColumnDefinition Name=\&quot;&quot;</font></font><font size="2"> &#43;</font><font size="2">i &#43; </font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;\&quot; Width=\&quot;100\&quot; /&gt;&quot;</font></font><font size="2">);</font><font size="2"> <p>}</p> <p>tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;/Grid.ColumnDefinitions&gt;&quot;</font></font><font size="2">);</p> tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;TextBlock Text=\&quot;Skills List \&quot; Grid.Column=\&quot;0\&quot;&quot;</font></font><font size="2">);</font><font size="2">tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot; FontWeight=\&quot;Bold\&quot; /&gt;&quot;</font></font><font size="2">);</font><font size="2"> <p>tempString.Append(stb.ToString()); // My combobox is already built in this.</p> <p>tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;Button Name = \&quot;btnAdd\&quot; Height=\&quot;20\&quot; Width=\&quot;100\&quot; Content=\&quot;Add Skill\&quot; &quot;</font></font><font size="2">);</p> tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot; VerticalAlignment=\&quot;Top\&quot; Grid.Column=\&quot;2\&quot; HorizontalAlignment=\&quot;Left\&quot; /&gt;&quot;</font></font><font size="2">);</font><font size="2"> <p>tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;/Grid&gt;&quot;</font></font><font size="2">);</p> tempString.Append(</font><font color="#a31515" size="2"><font color="#a31515" size="2">&quot;&lt;/ControlTemplate&gt;&quot;</font></font><font size="2">);</font><font size="2"> <p>//ti is my treeview item object;</font></p> <font color="#008000" size="2"><font color="#008000" size="2"></font></font><font color="#008000" size="2"><font color="#008000" size="2"> <p></font></font><font size="2">ti.Template = (</font><font color="#2b91af" size="2"><font color="#2b91af" size="2">ControlTemplate</font></font><font size="2">)</font><font color="#2b91af" size="2"><font color="#2b91af" size="2">XamlReader</font></font><font size="2">.Load(tempString.ToString());</p> </font> <p>&nbsp;</p> 2009-06-03T11:48:08-04:00229918http://forums.silverlight.net//p/99991/229918.aspx/1?Re+Adding+Treeview+Items+dynamically+in+codebehind+error+as+Invalid+XamlRe: Adding Treeview Items dynamically in codebehind! error as Invalid Xaml <p>You need to set the ti.Header=gd. Not ti.Items. </p> <p>You're doing it all wrong.&nbsp;Avoid generating TreeViewItems in code.<br> Always use HierarchicalDataTemplate whenever you can. </p> <p>If you don't know how HierarchicalDataTemplate works, feel free to read up @ <a href="http://silverlight.net/blogs/justinangel/archive/2008/11/18/silverlight-toolkit-treeview-treeviewitem-amp-hierarchaldatatemplate.aspx"> http://silverlight.net/blogs/justinangel/archive/2008/11/18/silverlight-toolkit-treeview-treeviewitem-amp-hierarchaldatatemplate.aspx</a>&nbsp;&nbsp;</p> <p>&nbsp;</p> 2009-06-06T01:15:38-04:00