c# - 具有多列的列表框

标签 c# wpf xaml listbox

我想在列表框中有多个列,其中包含我通过从数据库获取值动态创建的复选框。代码是这样的:

<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/

相关文章:

WPF:调整圆的大小,保持中心点而不是 TopLeft?

.net - 从 IValueConverter 获取对 ViewModel 的引用

c# - 如何在 ScrollViewer (WP7) 中执行 RenderTransform?

c# - 如何制作没有可点击屏幕的 .msi 文件(全自动)?

c# - 是否有没有唯一键的 Dictionary<Key,Value>?

wpf - 为什么在 DataTemplate 中使用控件时未设置 UserControl 中的自定义属性?

c# - 在 Windows Phone 8.1 XAML 中检测停用和应用程序关闭

c# - 数据 GridView : How to enable multi row select but disable multi cell select?

c# - 调试 ClickOnce 部署函数

c# - 允许鼠标消息的子集通过 WPF 中的控件