WrapPanel将项目放在一条长水平线(显示滚动条)而不是包裹线条

Shi*_*mmy 10 wpf grid xaml wrappanel itemspaneltemplate

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="Auto"/>
  <ColumnDefinition />
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
  <RowDefinition Height="Auto"/>
  <RowDefinition/>
  <RowDefinition Height="40"/>
</Grid.RowDefinitions>

<c:SearchTextBox Grid.ColumnSpan="2" .../>

<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="1">
  <ListBox 
    ItemsSource="{Binding Categories}" 
    IsSynchronizedWithCurrentItem="True" 
    ... />
</ScrollViewer>

<!-- Here is what I'm talking about:-->
<ListBox ItemsSource="{Binding Products}"
  IsSynchronizedWithCurrentItem="True" Grid.Column="1" Grid.Row="1">
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

我想要的是右栏中的项目应该被布置为填充窗口宽度,然后创建一个新的行,这正是WrapPanel的制作.
问题是,WrapPanel只在一行中显示项目,在下面显示水平滚动条,而所有项目在右侧"隐藏"超过窗口大小.

我怎么能防止这种情况?

Jin*_*ung 16

您需要使第二个ListBox的水平滚动条禁用.

<ListBox ItemsSource="{Binding}" 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...>
....
</ListBox>
Run Code Online (Sandbox Code Playgroud)

编辑

另外,你有理由将ScrollViewer用作第一个ListBox吗?为什么我要问的是ListBox内部已经有ScrollViewer,默认的Visibility是Auto.

  • 为了澄清为什么会发生这种情况,如果`ListBox`启用了水平滚动条(默认情况下它们),那么它会告诉它的子节点在无限水平空间中测量它们自己.通过删除滚动条,您可以强制`WrapPanel`使用有限的宽度,因此它实际上会换行. (4认同)