【问题标题】:WPF listview display items to columns based on height of windowWPF listview 根据窗口高度将项目显示到列
【发布时间】:2017-08-08 07:16:43
【问题描述】:

我一直在尝试创建一个列表视图来显示 MySQL 表,但我不知道如何使用 xaml 将项目排列到列中?

这就是我所拥有的

这就是我想做的:

另外,当我调整窗口大小时,我希望将项目排列到新列,如下所示:

这是我的 xaml 代码:

<ListView Grid.Column="1" 
          Grid.Row="1" 
          x:Name="DBTables" 
          ItemsSource="{ Binding Path=TABLENAME
                       , Source={StaticResource DBManagerResource} }" 
          MinHeight="280" 
          MinWidth="400">
      <ListView.ItemContainerStyle>
          <Style TargetType="ListViewItem">
              <EventSetter Event="MouseDoubleClick"
                           Handler="DBTableSelect_MouseDoubleClick" />
          </Style>
      </ListView.ItemContainerStyle>
</ListView>

【问题讨论】:

  • 您的示例图片不可用
  • @MightyBadaboom 不可用是什么意思?

标签: c# wpf xaml listview


【解决方案1】:

您需要WrapPanelOrientation="Vertical",使用ScrollViewer.VerticalScrollBarVisibility="Disabled" 禁用列表视图上的垂直滚动并配置ListView.ItemTemplate 以便在每个列表条目的前面显示图标。

<ListView MinHeight="280" MinWidth="400" ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <!--Replace rectangle by your icon-->
                <Rectangle Width="15" Height="15" Margin="5" Fill="Red" VerticalAlignment="Center"/>
                <ContentControl Content="{Binding}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【讨论】:

  • 非常感谢!!你帮了很多忙
  • 他刚才不是说我说的吗? :) (他确实为你写了,我会给他)
  • @Noctis 因为当其他答案出现时我已经完成了 90% 的答案,而且我觉得我的答案比其他答案更完整,所以我还是决定发布它;)
  • @grek40 不用担心,伙计。我只是觉得好笑。你给了一个很好的答案。
【解决方案2】:

听起来您正在寻找WrapPanel 或类似的东西。看看this article

您可能希望对项目的样式进行调整,以便它们显示图标和文本,但这应该不难实现。

【讨论】:

    【解决方案3】:

    此代码将在调整大小期间处理项目。也许对你有帮助。

    <ListView>
            <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.ItemTemplate>
                <DataTemplate>
                   <!-- Here you should add you item template, like image and text -->
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

    【讨论】:

      【解决方案4】:

      您需要更改 ListBox 的项目面板并禁用垂直滚动条

      <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled">        
           <ListBox.ItemsPanel>
              <ItemsPanelTemplate>
                  <WrapPanel Orientation="Vertical"/>
              </ItemsPanelTemplate>
          </ListBox.ItemsPanel>
      </ListBox>
      

      【讨论】:

        猜你喜欢
        • 2018-02-26
        • 2018-04-22
        • 1970-01-01
        • 2019-03-30
        • 2019-07-31
        • 1970-01-01
        • 1970-01-01
        • 2010-11-12
        • 1970-01-01
        相关资源
        最近更新 更多