views:

27

answers:

1
+1  Q: 

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 data context 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!

+2  A: 

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.

Jalfp
Ok, that sounds good, but how do I bind the Converter's output to a grid, with locations for each element somehow specified?
gmagana
You can change the ItemsPanelProperty of your ListBox to Grid. Then you can create a style that databound the Grid.Row and Grid.Column properties and set it to the ItemsContainerStyle.
Jalfp