【问题标题】:Listbox with multiple columns具有多列的列表框
【发布时间】:2012-05-21 10:24:22
【问题描述】:

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

<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 个复选框后更改列...我在这里使用了 wrap 面板,但它将所有复选框都垂直排列成一行。

那么现在该怎么办?

【问题讨论】:

  • 我不明白你的问题。你到底想达到什么目的? “我想在 5 个复选框后更改列”是什么意思

标签: c# wpf xaml listbox


【解决方案1】:

你应该试试这个:

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

【讨论】:

    猜你喜欢
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多