【问题标题】:WPF: multi-column listbox / listview?WPF:多列列表框/列表视图?
【发布时间】:2015-04-20 12:51:32
【问题描述】:

我想在 WPF 中创建一个在多列中显示数据的列表,例如 Explorer 在“小图标”视图模式下显示文件列表:

每个项目必须由DataTemplate 表示,并且滚动应该是水平的。如何制作这样的清单?

【问题讨论】:

  • 您是否已经检查过 itemscontrol 和 itemscontrol.ItemsPanelTemplate 以进行水平滚动
  • 是的,我试过这样做,但找不到让项目在多个垂直列中堆叠的方法。
  • uniformgrid 或 wrappanel 可以做到这一点

标签: wpf list listview


【解决方案1】:

您需要将ListBox 的ItemsPanel 更改为WrapPanel,并使用垂直Orientation 并禁用ListBox 上的垂直滚动条 像这样的:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled" ItemsSource="{Binding=MyItems}">
  <ListBox.ItemTemplate>
      <DataTemplate>
          <!--my item template-->
      </DataTemplate>
  </ListBox.ItemTemplate>
  <ListBox.ItemsPanel>
      <ItemsPanelTemplate>
          <WrapPanel Orientation="Vertical"/>
      </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>

【讨论】:

  • 很棒的实现 - @JSJ 也很好,但是没有网格标题也很好
【解决方案2】:

试试下面的方法。

<ListBox>
 <ListBox.ItemsPanel>
  <ItemsPanelTemplate>
   <UniformGrid Columns="3"/>
  </ItemsPanelTemplate>
 </ListBox.ItemsPanel>

 <DataTemplate>
      <!--my item template-->
 </DataTemplate>
</ListBox>

可以使用this链接

【讨论】:

    【解决方案3】:

    试试下面的方法。

    <ListView ItemsSource="{Binding Files}" >
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
                           ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}"
                           MinWidth="{Binding ItemWidth, RelativeSource={RelativeSource Self}}"
                           ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" />
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.View>
                    <GridView >
                        <GridView.Columns>
                            <GridViewColumn DisplayMemberBinding="{Binding Name}" />
                        </GridView.Columns>
                    </GridView>
                </ListView.View>
            </ListView>
    

    示例代码。

     public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                Files = new ObservableCollection<FileInfo>();
                var files = new System.IO.DirectoryInfo("C:\\Windows\\").GetFiles();
                foreach (var item in files)
                {
                    Files.Add(item);
                }
                this.DataContext = this;
            }
    
            public ObservableCollection<FileInfo> Files { get; set; }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      相关资源
      最近更新 更多