Iterating over dynamically generated controls and getting certain properties - silverlight

I have a UserControl that has an ItemsControl. This ItemsControl is bound to a list of objects. To display those objects, I have *DataTemplate*s that determine how the object is presented.
I want to be able to retrieve the current values of the controls, such as Text if it is a TextBox, or SelectedItem if it is a ComboBox.
How do I iterate over these dynamically generated controls? I do not know the names of the controls, as they are generated during runtime.
Thanks..

The simplest why I could think of besides searching the visual tree for the controls and theirs template properties is probably to bind the ItemsControl data source to an ObservableCollection. This why you can dynamically add any type of controls to the list.
Once you do that, you can easily check the type of each UIElement within the list. e.g; if it's a Textbox then get the text property by casting, etc.

Related

How to Bind Grid with Properties in Code behind?

I have a grid in a XAML file and I want to generate its RowDefinition dynamically. To do that I create a List<RowDefinition> in which I add 3-4 RowDefinitions. Now I have to bind this property to the grid in the XAML file. How do I do this?
If you want to host dynamic content (varying number of items), the Grid control might not be your best option. Try using an ItemsControl (which can be templated to look however you want) and bind your actual data against the ItemsSource property. If you post some more information about what kind of content you are looking to display I can give you an example of how to do it with an ItemsControl.

Display a variable-sized collection of TextBlocks/ComboBoxes in Silverlight 3

I have a list of textual descriptions, each of which a user must score on a scale of 1-5. The number of textual descriptions is variable, so I can't just define a static Grid in XAML.
The following image shows approximately what I'm after:
In ASP.NET, I could bind my list of text items to a Repeater control. For each row, the text would be displayed in a Label, and next to it would be a DropDownList that contains a static list of items. Is there a similar control available in Silverlight?
Should I be using one of these?
DataGrid
ListBox
Custom control derived from ItemsControl
(Other)
I solved this by using an ItemsControl, and within the ItemTemplate I placed the TextBlock and ComboBox controls.
The only wrinkle for me was that the ComboBox depended on a separate DataContext, so I had to resort to using the DataContextProxy described in this article

HeaderedContentControl ItemsSource

Why there is no HeaderedContentControl ItemsSource property?
How then can I databind to a list of objects to be represented by HeaderedContentControl?
Thanks
John
Because ItemsSource is all about multiple pieces of content, and HeaderedContentControl has one piece of content. Put another way, HeaderedContentControl's job is to present a single object, so it doesn't need a property whose job is to feed it multiple pieces of content.
Use HeaderedItemsControl instead, or (depending on your requirements) an ItemsControl where the DataTemplate is a HeaderedContentControl. (Though in the latter case you might as well just use a Panel and multiple elements within the panel -- the HCC isn't really buying you anything.) HIC's job is to present multiple items under a single header, and it's pretty flexible. For example, the framework uses HIC as the base class for both TreeViewItem (whose "header" is the item at hand, and whose "items" are the children of that item) and MenuItem (whose "header" is the menu item, and whose "items" are any sub-menu-items, for example in a drop-down or pop-out menu).
There's no ItemsSource property, since it can have only a single child (or two children if you count Header in), just like the class it inherits from - ContentControl. Use Content property instead.
You can find more about it on MSDN.
If you want to display list of objects within HeaderedContentControl, then just add ListBox as its Content and fill ListBox with objects.
Maybe you need a HeaderedItemsControl.
You can find a sample here.
You should be able to bind the collection to the Content property.
HeaderedContentControl by name itself is a collection of ContentControl each with Header.

Silverlight: how to display multiple properties of an object in a ListBox using DisplayMemberPath

I need to do this in code, not XAML.
Its not possible to programmatically create the content of a data template in the same way you might create Controls and add them to a UserControl.
Instead you will need to use System.Xml.Linq.XDocument to construct the DataTemplate as XML with DataTemplate being the root element. Once complete you can retrieve the XML string for the Root element and then use XamlReader.Load to get a constructed DataTemplate.
With a ListBox, you can specify a DataItemTemplate - just put whatever controls you want in that DataItemTemplate (e.g. you might want multiple TextBlocks), and bind them to whatever properties you want on the bound object.
To load the DataItemTemplate dynamically, you can use the same technique as illustrated in this blog post.

WPF bind to a list

I have a user control whose users I want to have them set a DataContext on to bind to a list of objects. In my control, however, I want to display that list in a Grid, but in a non-trivial order. The column/row of display of each element will be determined by some code I will write.
So I cannot do a straight databinding in my control, I need to write code that will read the DataContext and then do the processing to correctly position each element.
How would a relative WPF newbie go about doing that? I guess the part I don't understand is what the code in my usercontrol will look like to read the DataContext items so that I can process them.
EDIT: Clarification: I want to stress I want to bind to the XAML Grid element, not some other kind of grid or DataGrid. Thx!
One of the possible way to achieve that is to use a Converter. You could create a Converter which converts the input list into another list where the order has been changed. Then you could use a "normal" databinding which will use the Converter.

Resources