To change the ListView layout, use the ItemsControl.ItemsPanel property:
<StackPanel> <Button Content="I am IRON" /> <ListView ItemsSource="{Binding Path=MeasuringDeviceCommunicators}" > <ListView.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal"/> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Name}"/> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackPanel>
You can also use VirtualizingStackPanel instead of StackPanel , this can improve performance (if you have many items for the show).
Update
If you want to add a list to each element of the stack panel, you can do this by changing the ItemTemplate (which represents how each element is displayed).
For example:
<ListView.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="8"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding Path=Name}"/> <ListView Grid.Column="2" ItemsSource="{Binding Tags}"/> <Grid> </DataTemplate> </ListView.ItemTemplate>
To summarize, ListView has 3 interesting properties for determining how it is displayed:
Here is the code using all of these properties:
<ListView> <ListView.Items> <Button Content="Button 1"/> <Button Content="Button 2"/> <Button Content="Button 3"/> <Button Content="Button 4"/> </ListView.Items> <ListView.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Vertical" Background="LightCoral"/> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.Template> <ControlTemplate> <Border Background="LightBlue" BorderBrush="DarkGreen" BorderThickness="5"> <ItemsPresenter HorizontalAlignment="Right" /> </Border> </ControlTemplate> </ListView.Template> <ListView.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="8"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="This is an item : "/> <ContentPresenter Grid.Column="2" Content="{Binding}"/> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView>
Please note: this part:
<ListView.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Vertical" Background="LightCoral"/> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.Template> <ControlTemplate> <Border Background="LightBlue" BorderBrush="DarkGreen" BorderThickness="5"> <ItemsPresenter HorizontalAlignment="Right" /> </Border> </ControlTemplate> </ListView.Template>
equivalent to:
<ListView.Template> <ControlTemplate> <Border Background="LightBlue" BorderBrush="DarkGreen" BorderThickness="5"> <StackPanel Orientation="Vertical" Background="LightCoral" HorizontalAlignment="Right" IsItemsHost="True"/> </Border> </ControlTemplate> </ListView.Template>
Cรฉdric bignon
source share