我想在列表框中有多个列,其中包含我通过从数据库中获取值动态创建的复选框。代码是这样的:
<StackPanel Width="250" Height="80">
<ListBox Name="listBoxZone" ItemsSource="{Binding TheList}" Height="115" Background="Azure">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="CheckBoxZone" Content="{Binding TheText}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked" Margin="0,5,0,0"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是这里的复选框一个接一个地横向出现...我希望在5个复选框之后更改列...我在这里使用了包裹面板,但它将所有复选框全部复选框垂直排列。
那么现在该怎么办?
答案 0 :(得分:2)
你应该试试这个:
<ListBox Name="listBoxZone" ItemsSource="{Binding TheList}" Height="115" Background="Azure">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
ItemWidth="{Binding (ListBox.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListBox}}"
MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
ItemHeight="{Binding (ListBox.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListBox}}" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Name="CheckBoxZone" Content="{Binding TheText}" Tag="{Binding TheValue}" Checked="CheckBoxZone_Checked" Margin="0,5,0,0"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
此代码将在到达控件的宽度边界时换行,而项目数量达到5.如果这不符合您的要求,那么我建议您使用Grid而不是ListBox。