我想在列表框中有多个列,其中包含我通过从数据库获取值动态创建的复选框。代码是这样的:
<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 个复选框之后更改列...我在这里使用了环绕面板,但它将所有复选框垂直排列。
那么现在该怎么办呢?
最佳答案
你应该尝试这个:
<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。
关于c# - 具有多列的列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10683508/