Show controls in two columns (WPF)

I have a control associated with a collection of objects. An object has two properties (name, value) that I display in a text block and text field, respectively. The list is quite long, and I would like to show it in two columns. So my question is: is there a way to get an Itemscontrol element to display its elements in two columns?

PS: the collection is populated at runtime, and I don't know how many items I will need to show!

+6
wpf itemscontrol
source share
2 answers

I usually put elements in a WrapPanel and then set the panel width to 2x the width of the element. This gives me nice columns with an arbitrary number of elements. If the width of your element is different, I put each element in its own grid or fixed-width StackPanel.

+5
source share

Use a ListBox and specify the DataTemplate in which you place the TextBlock and TextBox. Use the bindings to fill both of them. See http://msdn.microsoft.com/en-us/library/ms742521.aspx for more details.

<ListBox x:Name="TheListBox"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" SharedSizeGroup="Key" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Name}" /> <TextBox Grid.Column="1" Text="{Binding Value }" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> TheListBox.ItemsSource = CollectionOfObjects; 
+11
source share

All Articles