我有WrapPanel和非常相似的项目在里面。也许WrapPanel是一个错误的容器,只是描述了我所拥有的。
我希望所有的项目都有相同的宽度;最小宽度为120。此外,我希望项目伸展,这是重点。
如果WrapPanel width为150 (小于2*最小值),则有一列,项目的宽度为150。
如果WrapPanel width为350 (小于3*最小值),则有两列,项目宽度为175 (350/2)。
如果WrapPanel宽度为370 (小于4*最小),则有三列,项目宽度为123 (370/3)。也可以是123的两项和124的一项,真的无关紧要。
问题是我如何才能获得这种行为?
发布于 2012-10-30 03:35:13
C#代码:
public MainWindow()
{
DataContext = this;
SomeList.Add(new SomeType());
SomeList.Add(new SomeType());
SomeList.Add(new SomeType());
SomeList.Add(new SomeType());
SomeList.Add(new SomeType());
InitializeComponent();
}
//SomeList Observable Collection
private ObservableCollection<SomeType> _someList =
new ObservableCollection<SomeType>();
public ObservableCollection<SomeType> SomeList { get { return _someList; } }
private void UniformGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
var grid = sender as UniformGrid;
if (grid.ActualWidth > 370) grid.Columns = 3;
else if (grid.ActualWidth > 150) grid.Columns = 2;
else grid.Columns = 1;
}
public class SomeType : DependencyObject
{
//Title Dependency Property
public string Title
{
get { return (string)GetValue(TitleProperty); }
set { SetValue(TitleProperty, value); }
}
public static readonly DependencyProperty TitleProperty =
DependencyProperty.Register("Title", typeof(string), typeof(SomeType),
new UIPropertyMetadata("unset yet"));
}XAML代码:
<Window.Resources>
<DataTemplate x:Key="SomeTemplate" DataType="{x:Type local:SomeType}">
<Border BorderBrush="Black" BorderThickness="2" CornerRadius="4">
<TextBlock Text="{Binding Title}"/>
</Border>
</DataTemplate>
</Window.Resources>
<ItemsControl
ItemsSource="{Binding SomeList}"
ItemTemplate="{StaticResource SomeTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid SizeChanged="UniformGrid_SizeChanged"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>https://stackoverflow.com/questions/13127905
复制相似问题