Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

Adding Treeview Items dynamically in codebehind! error... RSS

3 replies

Last post Jun 06, 2009 02:15 AM by JustinAngel

(0)
  • Tilwani

    Tilwani

    Member

    6 Points

    5 Posts

    Adding Treeview Items dynamically in codebehind! error as Invalid Xaml

    Jun 02, 2009 01:24 PM | LINK

    Hi All,

     

    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.  Not able to add a Grid Object: error an invalid XAML.

    How do i solve it: and if any one can give an example would be very useful.

    Following is the code.

    Grid gd = new Grid();

    RowDefinition rd1 = new RowDefinition();

    ColumnDefinition cd1 = new ColumnDefinition();

    ColumnDefinition cd2 = new ColumnDefinition();

    ColumnDefinition cd3 = new ColumnDefinition();

    gd.RowDefinitions.Add(rd1);

    gd.ColumnDefinitions.Add(cd1);

    gd.ColumnDefinitions.Add(cd2);

    gd.ColumnDefinitions.Add(cd3);

    TextBlock tb = new TextBlock();

    tb.Text = "Skills List";

    tb.VerticalAlignment = VerticalAlignment.Top;tb.HorizontalAlignment = HorizontalAlignment.Left;

     

    ComboBox cmb = new ComboBox();

    cmb.Name = "cmb" + ti.Header.ToString();

    cmb.ItemsSource = l;

    cmb.VerticalAlignment =
    VerticalAlignment.Top;

    cmb.HorizontalAlignment = HorizontalAlignment.Center;

    Button btn = new Button();

    btn.Name = "btn" + ti.Header.ToString();

    btn.Click += new RoutedEventHandler(btn_Click);

    btn.Content = "Add to my Skillset";

    btn.VerticalAlignment = VerticalAlignment.Top;

    btn.HorizontalAlignment = HorizontalAlignment.Right;

    Grid.SetRow(tb, 0);

    Grid.SetColumn(tb, 0);

    Grid.SetRow(cmb, 0);

    Grid.SetColumn(cmb, 1);

    Grid.SetRow(btn, 0);Grid.SetColumn(btn, 2);

    gd.Children.Add(tb);

    gd.Children.Add(cmb);

    gd.Children.Add(btn);

    //Treeviewitem object 

    ti.Items.Add(gd);

    Tree view

  • bajrachar

    bajrachar

    Member

    155 Points

    40 Posts

    Re: Adding Treeview Items dynamically in codebehind! error as Invalid Xaml

    Jun 03, 2009 01:19 AM | LINK

    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

    http://silverlight.net/samples/sl2/toolkitcontrolsamples/run/default.html

    Look for samples of Treeview and NestedHierarchicalTemplate.

    you will get a good idea of what i am talking about.

    Hope this was helpful

    Ravi

  • Tilwani

    Tilwani

    Member

    6 Points

    5 Posts

    Re: Adding Treeview Items dynamically in codebehind! error as Invalid Xaml

    Jun 03, 2009 12:48 PM | LINK

    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 "Click" 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????

    Following is the code

    StringBuilder tempString = new StringBuilder();

    string ns = "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"";

    tempString.Append("<ControlTemplate " + ns + ">");

    tempString.Append("<Grid>");

    tempString.Append("<Grid.ColumnDefinitions>");

    for (int i = 0; i < 3; i++)

    {

    tempString.Append(
    "<ColumnDefinition Name=\"" +i + "\" Width=\"100\" />");

    }

    tempString.Append("</Grid.ColumnDefinitions>");

    tempString.Append("<TextBlock Text=\"Skills List \" Grid.Column=\"0\"");tempString.Append(" FontWeight=\"Bold\" />");

    tempString.Append(stb.ToString()); // My combobox is already built in this.

    tempString.Append("<Button Name = \"btnAdd\" Height=\"20\" Width=\"100\" Content=\"Add Skill\" ");

    tempString.Append(" VerticalAlignment=\"Top\" Grid.Column=\"2\" HorizontalAlignment=\"Left\" />");

    tempString.Append("</Grid>");

    tempString.Append("</ControlTemplate>");

    //ti is my treeview item object;

    ti.Template = (ControlTemplate)XamlReader.Load(tempString.ToString());

     

    tree view hierarchy insert runtime TreeViewItem Tree view Wrap Panel treeview ItemContainerGenerator

  • JustinAngel

    JustinAngel

    Contributor

    4455 Points

    606 Posts

    Re: Adding Treeview Items dynamically in codebehind! error as Invalid Xaml

    Jun 06, 2009 02:15 AM | LINK

    You need to set the ti.Header=gd. Not ti.Items.

    You're doing it all wrong. Avoid generating TreeViewItems in code.
    Always use HierarchicalDataTemplate whenever you can.

    If you don't know how HierarchicalDataTemplate works, feel free to read up @ http://silverlight.net/blogs/justinangel/archive/2008/11/18/silverlight-toolkit-treeview-treeviewitem-amp-hierarchaldatatemplate.aspx  

     

    --
    Justin Angel,
    Twitter @ http://twitter.com/JustinAngel
    Blog @ http://JustinAngel.net