David David -3 years ago 154
C# Question

Using an ItemsControl inside of a ListView ItemTemplate with XAML

So, a bit of a crazy request. I need a list of items, which I plan to use a

for, but inside each element, I need another "List" so-to-speak that will expand horizontally.

Here is a crude drawing of the basic idea I'm going for:

Crude Drawing of desired look
For my code, this is what I've tried, but it's not working. (And yes, I'm purposely not using binding yet so I can just use the design view to get everything looking right)

<ItemsControl ItemsSource="{Binding}">
<Grid Grid.IsSharedSizeScope="True">
<ColumnDefinition Width="Auto" SharedSizeGroup="KpiColumn0" />
<ColumnDefinition Width="Auto" SharedSizeGroup="KpiColumn1" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />

<StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="0">
<TextBlock Text="Test" />
<TextBlock Text="Test" FontSize="8" Margin="0,-4,0,0" />

<TextBlock Grid.Row="0" Grid.Column="2" Text="Test" />
<TextBlock Grid.Row="1" Grid.Column="0" Text="Test" />
<TextBlock Grid.Row="1" Grid.Column="2" Text="Test" />
<MenuItem />

How can I use a
to hold a list of items in each of its item, that expand horizontally, based on how many elements are in the
that each
is bound to?

Answer Source

Add this to your inner ItemsControl and the items will be shown in horizontal alignment.

        <StackPanel Orientation="Horizontal"/>

Note that you won't be able to use the designer view for this. You'll need to remove the ListView.Items that you have, and instead use binding or code-behind to have it show when running.

Something like this should work, if you give the ListView a Name of LV:

LV.ItemsSource = new List<List<int>>
    new List<int> {1,2,3,4, },
    new List<int> {1,2,3,4, }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download