Build TreeViewItem dynamically [modified]
-
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
-
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
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.
-
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
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
-
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.
-
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
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.
-
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
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);