2017-06-22 2 views
0

Ich habe eine Ansicht, die Informationen anzeigen und testen sollte, fügte ich testdata hinzu. Wenn ich jedoch zu viele Elemente für den Bildschirmbereich hinzufüge, wird die vertikale Bildlaufleiste nicht angezeigt.ListBox Vertikale Bildlaufleiste wird nicht angezeigt (WPF)

Das ist mein XML-Code:

<UserControl x:Class="not relevant" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="not relevant" 
    mc:Ignorable="d" 
    Width="250"> 

<Border Background="{DynamicResource BG}" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1"> 
    <StackPanel> 
     <Grid> 
      <Label Height="30" Content="Geöffnete Designs" HorizontalContentAlignment="Center"></Label> 
      <Separator Background="DarkGray"/> 
      <ListBox Background="{DynamicResource BG}" BorderThickness="0" VerticalAlignment="Stretch" > 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
       <ListBoxItem> 
        <Label Content="Hallo"></Label> 
       </ListBoxItem> 
      </ListBox> 
     </Grid> 
    </StackPanel> 
</Border> 

Ich habe versucht, einen Scroll-Viewer anstatt das Hinzufügen, aber das funktioniert nicht richtig. Mache ich etwas falsch oder was könnte ich das Problem?

Antwort

2

Entfernen Sie einfach die StackPanel in Ihrem XAML-Code. Du brauchst es nicht und machst nur Probleme. Ich würde vorschlagen, Ihr Styling-Layout zu ändern, weil Sie kein Grid verwenden, wie Sie sollten.

Mindestens fügen Sie einige RowDefinitions UI-Layout, wenn Sie die Label oben und die List unter dem Label zeigen wollen.

könnte wie folgt aussehen

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <Label Grid.Row="0" 
      Height="30" 
      Content="Geöffnete Designs" 
      HorizontalContentAlignment="Center" /> 
    <Separator Grid.Row="1" 
       Background="DarkGray" /> 
    <ListBox Grid.Row="2" 
      BorderThickness="0" 
      VerticalAlignment="Stretch"> 

Result

+0

ziemlich neu in WPF. Vielen Dank für die weitere Erklärung in Ihrer Bearbeitung. Sollte wahrscheinlich ein Buch anstelle von Tutorials lesen, die nur Code ohne weitere Erklärung zeigen – fbueckle

+0

Ihre Begrüßung, wir alle begannen einmal :) Viel Glück beim Lernen WPF –

0

einfach einen Scroll-Viewer in Ihrem XAML hinzufügen

<ScrollViewer> 
    <Border BorderThickness="1"> 
     <StackPanel> 
      <Grid> 
       <Label Height="30" Content="Geöffnete Designs" HorizontalContentAlignment="Center"></Label> 
       <Separator Background="DarkGray"/> 
       <ListBox BorderThickness="0" VerticalAlignment="Stretch" > 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
        <ListBoxItem> 
         <Label Content="Hallo"></Label> 
        </ListBoxItem> 
       </ListBox> 
      </Grid> 
     </StackPanel> 
    </Border> 
</ScrollViewer> 
+0

Werfen Sie einen Blick auf meine Antwort. Entfernen Sie einfach das 'StackPanel' und benutzen Sie das' Grid' wie Sie es verwenden sollten und Sie brauchen keinen Scroll Viewer hinzuzufügen;) –

+0

Ja, wie von @Mighty Badaboom vorgeschlagen, funktionieren ScrollViewers nicht gut mit StackPanels. Dies liegt daran, dass ein StackPanel seine untergeordneten Objekte mit einem unendlichen Platz misst: https://stackoverflow.com/questions/41140287/horizontal-croll-for-stackpanel-doesnt-work/41140885#41140885 – mm8

Verwandte Themen