【问题标题】:How to set the number of ListView columns dynamically如何动态设置 ListView 的列数
【发布时间】:2011-12-15 11:09:16
【问题描述】:

我有一个包含两个用户控件的窗口,一个带有列表视图的用户控件和一个带有其他控件的用户控件。

类似这样的:

+-------------------------------------------------+
| header stuff                                    |
+---------------------------+---------------------+
| usercontrol with listview | another usercontrol |
+-------------------------------------------------+
| footer stuff                                    |
+-------------------------------------------------+

问题:在分辨率较低的屏幕上,只能看到 3 列中的 2 列,您需要滚动才能看到第三列。我想通过根据宽度动态设置是否显示 1,2 或 3 列来避免水平滚动。另一个问题是名称可以 很长,所以listview中item的宽度都和最长的名字一样宽。

列表视图代码:

<ListView Name="lstContacts" 
 ItemsSource="{Binding Path=Contacts}" 
 IsSynchronizedWithCurrentItem="True"
 ItemContainerStyle="{StaticResource RoundedItem}"
 SelectionMode="Single"
 HorizontalContentAlignment="Center"    
 VerticalAlignment="Top"
 HorizontalAlignment="Stretch" >
<ListView.ItemTemplate>
    <DataTemplate>
            <StackPanel Orientation="Vertical" Margin="3">
                <TextBlock FontWeight="Bold" FontSize="20" Text="{Binding     Path=Identifier}" HorizontalAlignment="Center" />
                <TextBlock FontSize="16" Text="{Binding Path=Name}"     HorizontalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="3" />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
</ListView>

关于如何解决这个问题的任何想法?

也许列表视图是个糟糕的选择?

谢谢。

【问题讨论】:

    标签: wpf listview


    【解决方案1】:

    您可以使用WrapPanel 作为ItemsPanel

    <ItemsPanelTemplate>
      <WrapPanel />
    </ItemsPanelTemplate>
    

    请注意,没有原生虚拟化 WrapPanel,因此这对于大量项目集合而言效果不佳。

    【讨论】:

      【解决方案2】:

      因为你的名字可能很长,你可以: 1-使用具有文本换行和最大/最小高度的文本框。 2-减小名称的字体大小。 3-将名称放在 ViewBox 中,以便它们始终适合。 (也换行)

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-15
        • 2019-12-06
        • 2013-10-07
        • 1970-01-01
        • 2012-11-14
        相关资源
        最近更新 更多