Search code examples
c#xamllistbox

How resize a ListBox?


With .net I want to make a ListBox that populates according to a file. The populating goes well, but I can't go down properly in the ListBox. It goes beyond the height of my window, the problem probably comes from the fact that I'm using the height of the DockPanel. How can I avoid this problem?

<DockPanel x:Name="DockPanel_Color">
    <StackPanel DockPanel.Dock="Right">
        <Label Content="Couleur RGB"/>
        <Border x:Name="Border_SelectedColor" Background="red" Width="100" Height="100" BorderThickness="2" BorderBrush="Black" CornerRadius="10"/>
    </StackPanel>
    <StackPanel x:Name ="test" Margin="5" Height="{Binding ActualHeight, ElementName=DockPanel_Color}">
        <TextBlock>Couleurs</TextBlock>
        <ListBox x:Name="ListBox_Couleurs" SelectionChanged="ListBox_Couleurs_SelectionChanged">
            <ListBox.MaxHeight>
                <Binding Path="ActualHeight" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DockPanel}}" />
            </ListBox.MaxHeight>
        </ListBox>
    </StackPanel>
</DockPanel>

My Application :

enter image description here

I tried without defining a height, with a scrollerViewer, using the height of the "test" StackPanel. But nothing works. And setting a fixed height doesn't resize the window.


Solution

  • I would try just using another dock panel. The last element in a dock panel fills the remaining space. If you need a scroll bar, just add a ScrollViewer:

    <DockPanel x:Name ="test" Margin="5">
            <TextBlock DockPanel.Dock="Top">Couleurs</TextBlock>
            <ScrollViewer HorizontalScrollBarVisibility="Auto">
                   <ListBox x:Name="ListBox_Couleurs" SelectionChanged="ListBox_Couleurs_SelectionChanged"/>
            </ScrollViewer>
    </DockPanel>