Reputation: 117
I don't know how to put element below listbox. Listbox should occupy the whole screen.
<StackPanel>
<ListBox>
<ListBoxItem>//Elements from ViewModel</ListBoxItem>
</ListBox>
<TextBlock>Additional elment</TextBlock>
</StackPanel>
But scroll doesn't works. I can not assign a high, because it is unknown
I would have the following result:
Upvotes: 1
Views: 119
Reputation: 169200
You could for example use a DockPanel:
<DockPanel>
<TextBlock DockPanel.Dock="Bottom">Additional element</TextBlock>
<ListBox>
<ListBoxItem>//Elements from ViewModel</ListBoxItem>
</ListBox>
</DockPanel>
The last element in the DockPanel, i.e. the ListBox in this case, will fill the remaining space unless you set the LastChildFill property of the DockPanel to false: https://msdn.microsoft.com/en-us/library/system.windows.controls.dockpanel.lastchildfill(v=vs.110).aspx
StackPanels don't work very well with scrollbars. Please refer to my answer to the below question for more information about this.
Horizontal scroll for stackpanel doesn't work
Your solution will make additional element be will at the bottom of the window, not below the last item in the listbox. My problem is different.
Use a Grid then:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ListBox Grid.Row="0" />
<TextBlock Grid.Row="1">Additional element</TextBlock>
<!-- Grid.Row="2" = the rest of the window content... -->
</Grid>
i want the additional element to scroll with the rest - it is a button
Use a CompositeCollection as the ItemsSource for the ListBox then instead of binding directly to your source property:
<ListBox>
<ListBox.Resources>
<CollectionViewSource x:Key="itemsSource" Source="{Binding YourViewModelSourceCollection}" />
</ListBox.Resources>
<ListBox.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource itemsSource}}" />
<TextBlock>Additional element</TextBlock>
</CompositeCollection>
</ListBox.ItemsSource>
</ListBox>
Upvotes: 1