首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展WrapPanel项目

扩展WrapPanel项目
EN

Stack Overflow用户
提问于 2012-10-30 02:55:16
回答 1查看 1.6K关注 0票数 3

我有WrapPanel和非常相似的项目在里面。也许WrapPanel是一个错误的容器,只是描述了我所拥有的。

我希望所有的项目都有相同的宽度;最小宽度为120。此外,我希望项目伸展,这是重点。

如果WrapPanel width为150 (小于2*最小值),则有一列,项目的宽度为150。

如果WrapPanel width为350 (小于3*最小值),则有两列,项目宽度为175 (350/2)。

如果WrapPanel宽度为370 (小于4*最小),则有三列,项目宽度为123 (370/3)。也可以是123的两项和124的一项,真的无关紧要。

问题是我如何才能获得这种行为?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-30 03:35:13

C#代码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
<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>
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13127905

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档