【发布时间】:2011-09-30 03:30:17
【问题描述】:
我需要一个水平组织的ItemsControl,它将其所有项目限制为相同的宽度。我正在使用的项目是UserControls,并构建了一个自动调整大小的TextBlock,显示一个int 值(包含在依赖属性中),周围有一个Border。问题是较小的值会导致更窄的项目,我需要所有项目都相同。我已经考虑了一些解决方案,但我似乎无法做出任何工作。
第一种是将ItemsPanel 模板设置为Grid。这样我就可以使用代码隐藏根据数据源生成所需的列数,并将所有列宽设置为*。难题在于如何为每个项目设置网格列附加属性。
第二种解决方案是将ItemsPanel 模板设置为StackPanel。这会自动正确排列项目,但我无法将每个项目的宽度设置为最宽项目的宽度。
最后一个解决方案是将ItemsPanel 模板设置为UniformGrid,并在数据源更改时设置列数。这将自动正确排列项目并提供统一的宽度。我的问题是我根本无法显示任何项目。我试过手动添加按钮,它们显示得很好。我的UserControl 不会出现。我在下面列出了这个解决方案。
<Window x:Class="Learning_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Learning_WPF"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="DateTape" Height="176" Width="500">
<Window.Resources>
<my:DateList x:Key="dateList" CollectionChanged="DateList_CollectionChanged" />
</Window.Resources>
<ItemsControl x:Name="itemsControl1" ItemsSource="{Binding Source={StaticResource dateList}, Path=/}" Grid.Row="1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid x:Name="daysGrid" Rows="1" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Window>
public partial class MainWindow : Window
{
UniformGrid daysGrid;
DateList dateList;
public MainWindow()
{
InitializeComponent();
daysGrid = (UniformGrid)itemsControl1.ItemsPanel.LoadContent();
dateList = (DateList)FindResource("dateList");
dateList.Fill(DateTime.Today, DateTime.Today.AddDays(10));
}
private void DateList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
daysGrid.Columns = dateList.Count;
}
}
public class DateList : ObservableCollection<Date>
{
public void Fill(DateTime first, DateTime last)
{
// implementation fills the array with all of the days between first and last, inclusively
}
}
也许有更好的方法来实现我想要实现的目标(可能比使用控件更图形化)...
【问题讨论】:
-
你试过 UniformGrid。试试看吧?
-
我确实尝试过,但我之前遇到过麻烦。从那以后,我进行了一些重组。我再试一次...
-
在我的情况下,我有一个集合,它具有属性 DispayText 并且绑定到按钮内容。为此,我创建了转换器,它将通过识别要显示的最高字符串来获取按钮的宽度,并在 UniformGrid 中设置 Row=1
-
@KishoreKumar,请在下面查看我的解决方案。
标签: wpf grid itemscontrol stackpanel