Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Build TreeViewItem dynamically [modified]

Build TreeViewItem dynamically [modified]

Scheduled Pinned Locked Moved WPF
csharpvisual-studiowpfhelptutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    McCombi
    wrote on last edited by
    #1

    Hi all, I need to build TreeViewItems dynamically in Silverlight4, C#.net, Visual Studio 2010 Premium. Currently the TreeView consists of only a lot of TextBlocks, and I need to put a CheckBox in front of each TextBlock dynamically. This has to be done in code-behind, not in the xaml. I have done a few Google searches, but could not find anything that I could use to guide me to do this. Please provide me with links to articles/demos/discussions/etc that will help me. Some code snippits would be appreciated too... Thanks :cool:

    modified on Friday, August 5, 2011 7:45 AM

    S M M 4 Replies Last reply
    0
    • M McCombi

      Hi all, I need to build TreeViewItems dynamically in Silverlight4, C#.net, Visual Studio 2010 Premium. Currently the TreeView consists of only a lot of TextBlocks, and I need to put a CheckBox in front of each TextBlock dynamically. This has to be done in code-behind, not in the xaml. I have done a few Google searches, but could not find anything that I could use to guide me to do this. Please provide me with links to articles/demos/discussions/etc that will help me. Some code snippits would be appreciated too... Thanks :cool:

      modified on Friday, August 5, 2011 7:45 AM

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      You should explain what you are trying to do. Telling us you need to dynamically build a UI from the code behind sounds like you are headed down the wrong path. I just wrapped up a customized TreeView that I wrote that does exactly what you are talking about... added checkbox and image support to the stock WPF TreeView. None of my code involves dynamically building the UI from the code behind. Its all in XAML via triggers, etc.

      M 1 Reply Last reply
      0
      • M McCombi

        Hi all, I need to build TreeViewItems dynamically in Silverlight4, C#.net, Visual Studio 2010 Premium. Currently the TreeView consists of only a lot of TextBlocks, and I need to put a CheckBox in front of each TextBlock dynamically. This has to be done in code-behind, not in the xaml. I have done a few Google searches, but could not find anything that I could use to guide me to do this. Please provide me with links to articles/demos/discussions/etc that will help me. Some code snippits would be appreciated too... Thanks :cool:

        modified on Friday, August 5, 2011 7:45 AM

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Sledge has got it right, if you are using code to create a UI element you are doing it WRONG. Build you template in xaml with a stackpanel containing a checkbox and a textblock, bind them to your data collection. Your data collection may be manipulated from the code but not your UI element.

        Never underestimate the power of human stupidity RAH

        1 Reply Last reply
        0
        • S SledgeHammer01

          You should explain what you are trying to do. Telling us you need to dynamically build a UI from the code behind sounds like you are headed down the wrong path. I just wrapped up a customized TreeView that I wrote that does exactly what you are talking about... added checkbox and image support to the stock WPF TreeView. None of my code involves dynamically building the UI from the code behind. Its all in XAML via triggers, etc.

          M Offline
          M Offline
          McCombi
          wrote on last edited by
          #4

          Done in XAML via triggers? I have never heard of triggers in XAML. What is it, and how are they used? But I guess that's for another thread, huh. I'll Google it... :thumbsup:

          1 Reply Last reply
          0
          • M McCombi

            Hi all, I need to build TreeViewItems dynamically in Silverlight4, C#.net, Visual Studio 2010 Premium. Currently the TreeView consists of only a lot of TextBlocks, and I need to put a CheckBox in front of each TextBlock dynamically. This has to be done in code-behind, not in the xaml. I have done a few Google searches, but could not find anything that I could use to guide me to do this. Please provide me with links to articles/demos/discussions/etc that will help me. Some code snippits would be appreciated too... Thanks :cool:

            modified on Friday, August 5, 2011 7:45 AM

            M Offline
            M Offline
            McCombi
            wrote on last edited by
            #5

            Thanks for the replies. Additional Info: The UserControl that contains the TreeView is used on another page too, but NOT with the CheckBox. The functionality there requires the user to actually select the appropriate TreeViewItem. In the new scenario, the TreeView is be populated with exactly the same data is on the other page, but with the additional CheckBox for each TreeViewItem where the user is allowed to select multiple CheckBoxes in the TreeView. My idea was to just add the CheckBox to each TreeViewItem dynamically as each is built, and before it is added to the TreeViewItems collection. Question: Should I put another grid in my XAML with exactly the same methods etc as for the original TreeView, but with the additional CheckBox as part of the TreeViewItemTemplate? Then whenever this altered TreeView is needed, I toggle the visibility of the 2 grids in my XAML? Thanks again for any directional advice and alternative suggestions.

            1 Reply Last reply
            0
            • M McCombi

              Hi all, I need to build TreeViewItems dynamically in Silverlight4, C#.net, Visual Studio 2010 Premium. Currently the TreeView consists of only a lot of TextBlocks, and I need to put a CheckBox in front of each TextBlock dynamically. This has to be done in code-behind, not in the xaml. I have done a few Google searches, but could not find anything that I could use to guide me to do this. Please provide me with links to articles/demos/discussions/etc that will help me. Some code snippits would be appreciated too... Thanks :cool:

              modified on Friday, August 5, 2011 7:45 AM

              M Offline
              M Offline
              McCombi
              wrote on last edited by
              #6

              Hi all, This is how I did it. I hope it helps someone in the future. :cool:

              Microsoft.Windows.Controls.TreeViewItem treeitem = new Microsoft.Windows.Controls.TreeViewItem();
              StackPanel sp = new StackPanel();
              sp.Orientation = Orientation.Horizontal;
              CheckBox cb = new CheckBox();
              sp.Children.Add(cb);
              sp.Children.Add(treeitem);

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups