1

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: enter image description here

WymyslonyNick
  • 117
  • 3
  • 19

1 Answers1

1

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>
Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
  • 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. – WymyslonyNick Dec 29 '16 at 12:56
  • Do you mean that you want to put the additional element *inside* the ListBox? Then you should add another object to the ListBox itself. If you want to put the element below the ListBox you could also use a Grid. I edited my answer. – mm8 Dec 29 '16 at 12:57
  • I don't want to have the additional element on the list - but I want an additional element behaved in this way. Your solution with grid will make that any scrolls will not be visible. – WymyslonyNick Dec 29 '16 at 13:04
  • Do you want the additional element to scroll with the rest of the items in the ListBox or what? – mm8 Dec 29 '16 at 13:08
  • Yes, i want the additional element to scroll with the rest - it is a button :/ – WymyslonyNick Dec 29 '16 at 13:13
  • Use a CompositeCollection as the ItemsSource for the ListBox instead of binding directly to your source property then. I edited my answer again. – mm8 Dec 29 '16 at 13:19