A Grid
is a layout container, not a control. You want to use a DataGrid
, which will allow you to set the ItemsSource
to your Mycollection
. You can then either let it create the columns for you, or you take control and specifically define what columns you want and how they should be shown.
Draw a grid dynamically xaml c#
Question
I want a grid in xaml tha can show me a collection of objects how should i proceed ?
I already binded these :
C# // Collection.DataContext = Mycollection;
xaml // Grid.RowSpan="{Binding getSizeofcollection}"
My collection contains all the same kind of object. I want to show 1 object/row
Is that possible to create a template of how to show an object in a row then use it in the grid ?
Thanks for your advices
Edit : In fact i can't bind to hashtables, but i can bind to a List that's the source of my problems. Now i'm currently trying to transform my hashtable in a List
Edit n°2 : Ok i convert hashtable to a list correctly, but this xamlparse Exception is still here
Solution
OTHER TIPS
I'm sorry, I'm finding your question is a bit unclear as to what you're asking for.
Based on your comment here
I have a List of the same objects and i want each objects and their properties in a line of the DataGrid/StackPanel or something So, 20 objects, 20 lines, A collumn for each property
It sounds like you just want a DataGrid.
<DataGrid x:Name="Collection" ItemsSource="{Binding }" />
The default behavior of a DataGrid
is to create a Row for each item, and to auto-generate a column for each property of the item. So if you have 20 objects of type MyClass
in a list, and each contained 3 properties, it would auto-generate a grid with 20 rows and 3 columns.
However if I have misunderstood you and you're actually looking to create a horizontal StackPanel containing all your items in a line, and you don't need the selection/sorting capabilities that a DataGrid provides, I'd recommend using an ItemsControl instead.
This is simply a control that will iterate over a collection, and draw each item in the collection with whatever template you specify. All the items get wrapped in a <ContentPresenter>
and placed in whatever layout container you specify (default is a vertical StackPanel).
Since you said you want all items in a horizontal line, the best ItemsPanelTemplate
would probably be a StackPanel
with it's orientation set to Horizontal
.
Your XAML would end up looking like this:
<ItemsControl x:Name="Collection" ItemsSource="{Binding }">
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- ItemTemplate -->
<ItemsControl.ItemTemplate>
<DataTemplate>
<!-- // put whatever XAML you want to use to draw each item here -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
You can use some custom AttachedProperty.
Something like this:
<Grid Grid.Row="1"
commonUI:GridExtension.ItemsSource="{Binding ItemsSource}"
commonUI:GridExtension.Rows="{Binding RowDefinitions}"
commonUI:GridExtension.Columns="{Binding ColumnDefinitions}"/>
For the documentation: http://msdn.microsoft.com/en-us/library/ms749011(v=vs.110).aspx
But in your case, a simple ListBox or ItemsControl seem to be more appropriate...