c# - 水平列表框多行(WPF)
问题描述
我想实现一个带有多行的水平列表框。
使用 WrapPanel:
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
我得到以下结果:
但我正在寻找这个结果:
我该如何实施?
解决方案
根据您的评论,UniformGrid
每当调整窗口大小时,我都会调整行数/列数。
MainWindow.xaml:
<Grid x:Name="Container">
<ListBox HorizontalContentAlignment="Stretch">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid x:Name="ItemsGrid" Loaded="ItemsGrid_Loaded" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
名字和事件很重要。它们用于调整代码文件中的列/行。
主窗口.xaml.cs
public partial class MainWindow : Window
{
private UniformGrid itemsGrid;
public MainWindow()
{
InitializeComponent();
}
private void ItemsGrid_Loaded(object sender, RoutedEventArgs e)
{
// Set reference and adjust columns/rows once the UniformGrid is loaded.
itemsGrid = sender as UniformGrid;
((UniformGrid)sender).Columns = (int)(Container.ActualWidth / 250);
((UniformGrid)sender).Rows = (int)(Container.ActualHeight / 75);
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
// Adjust number of columns/rows whenevner the window is resized.
if (itemsGrid != null)
{
itemsGrid.Columns = (int)(Container.ActualWidth / 250);
itemsGrid.Rows = (int)(Container.ActualHeight / 75);
}
}
}
这肯定可以清理一下,但它似乎可以完成这项工作。可能有更优雅的解决方案,但我找不到任何...
推荐阅读
- javascript - Google My Business API 类别问题
- java - java.io.UnsupportedEncodingException:ISO-8859-8,文本/xml
- sql - 在sql中的concat col中获取相同行数
- mysql - 表达式 #3 上的错误代码 1055 - 因为重复的行?
- amazon-web-services - 是否有快速检查 S3 服务器访问日志是否正常工作的方法?
- php - 如何使用 Xampp 服务器增加 phpMyAdmin 中的 PHP 限制?
- java - 带有安全(HTTPS)客户端抛出异常的 Eureka 服务器中的服务注册表
- bamboo - Bamboo JDK 14 功能
- algorithm - 为数组元素选择 2 个可能的数字以获得总和 's'
- java - Corda - 在 Corda 保险库中存储字符串数据是否有任何限制?