WPF:WrapPanelを使用したListBox、垂直スクロールの問題
質問
WrapPanel内に画像を表示したいListBoxを持つUserControl(以下のXAML)があります。画像は1行に収まるように表示され、次の行に折り返されます。 ListBoxがウィンドウ内の使用可能なスペースよりも大きくなると、垂直スクロールバーが表示されません。つまり、コンテンツがクリップされます。 ListBoxに固定の高さを設定すると、スクロールバーが表示され、期待どおりに機能します。このリストボックスを使用可能なスペースまで拡大し、垂直スクロールバーを表示するにはどうすればよいですか?このコントロールは、メインウィンドウのグリッド内のStackPanel内にあります。 StackPanelをScrollViewer内にラップすると、スクロールバーが表示されますが、ListBoxの上にあるUserControlにいくつかのコントロール(画像サイズ" zoom"など)を追加する場合、これはあまり良い解決策ではありません。私は彼らに画像をスクロールさせたくありません。
ありがとう!! :)
<UserControl x:Class="GalleryAdmin.UI.GalleryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="LightGray" Margin="5" >
<StackPanel Margin="5">
<Image Source="{Binding Path=LocalThumbPath}" Height="100" />
<TextBlock Text="{Binding Path=Name}" TextAlignment="Center"></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
解決 2
まあ、私はついに解決策を見つけました。次のようなプレースホルダーパネルにUserControlを追加していました。
<ScrollViewer Margin="20" >
<StackPanel Name="contentPanel"></StackPanel>
</ScrollViewer>
ただし、代わりにグリッドに切り替えたとき、物事は私が望むように機能し始めました:
<Grid Name="contentPanel" Margin="20" />
Gridが行っているように、デフォルトではStackPanelがすべての垂直スペースを占有しないことに関係していると思います。
他のヒント
ItemPanelTemplateをオーバーライドすることをお勧めします:
<Grid>
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>listbox item 1</ListBoxItem>
<ListBoxItem>listbox item 2</ListBoxItem>
<ListBoxItem>listbox item 3</ListBoxItem>
<ListBoxItem>listbox item 4</ListBoxItem>
<ListBoxItem>listbox item 5</ListBoxItem>
</ListBox>
次の設定を行うだけで問題は解決しました。
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
この問題に関するいくつかの質問に目を通しましたが、これは古いスレッドですが、この質問に答えてくれましたが、明確にするために...
レイアウトGRIDは、このようなほとんどの問題に対する答えです。適切なListBox / WrapPanel操作を取得して使用可能なスペースを埋めるために、次のコードがトリックを実行します。
<Grid Grid.Row="1" MaxHeight="105">
<ListBox ItemTemplate="{DynamicResource StoreGroupTemplate01}" ItemsSource="{Binding StoreGroupHeader}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
これを別のグリッドに配置して、画面の下部にリストを配置します(つまり、Grid.Row =&quot; 1&quot;)。MaxHeightを調整(または削除)して、垂直スクロールバーが表示されます。
ScrollViewer内にリストボックスを配置し、scrollviewerのVerticalScrollBarVisibilityプロパティを&quot; Auto&quot;に設定します
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
<ListBox Name="itemListBox" BorderThickness="0" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="LightGray" Margin="5" >
<StackPanel Margin="5">
<Image Source="{Binding Path=LocalThumbPath}" Height="100" />
<TextBlock Text="{Binding Path=Name}" TextAlignment="Center"></TextBlock>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</ScrollViewer>
HTH