不,我怀疑您尝试过 WrapPanel。 WinRT 中没有。
使用 VariableSizedWrapGrid 你会得到类似这样的结果:
使用 WrapGrid 你会得到类似这样的结果:
快!诀窍?
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
}
public class MyViewModel
{
public MyViewModel()
{
var _Random = new Random((int)DateTime.Now.Ticks);
var _Colors = typeof(Windows.UI.Colors)
// using System.Reflection;
.GetRuntimeProperties()
.Select((c, i) => new
{
Title = c.Name,
Color = (Windows.UI.Color)c.GetValue(null),
ColSpan = _Random.Next(20, 300),
RowSpan = _Random.Next(20, 300),
});
this.Groups = new System.Collections.ObjectModel.ObservableCollection<object>();
this.Groups.Add(new { Title = "Mostly Red", Children = _Colors.Where(x => x.Color.R > 200) });
this.Groups.Add(new { Title = "Mostly Green", Children = _Colors.Where(x => x.Color.G > 200) });
this.Groups.Add(new { Title = "Mostly Blue", Children = _Colors.Where(x => x.Color.B > 200) });
}
public System.Collections.ObjectModel.ObservableCollection<object> Groups { get; private set; }
}
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.DataContext>
<local:MyViewModel />
</Grid.DataContext>
<Grid.Resources>
<CollectionViewSource
x:Name="MyCsv"
IsSourceGrouped="True"
ItemsPath="Children"
Source="{Binding Groups}"
d:Source="{Binding Groups, Source={d:DesignInstance Type=local:MyViewModel, IsDesignTimeCreatable=True}}" />
</Grid.Resources>
<GridView ItemsSource="{Binding Source={StaticResource MyCsv}}">
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Height="20" Width="20" Fill="{Binding Brush}" Margin="0,0,10,0" />
<TextBlock FontSize="40" Text="{Binding Title}" />
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<local:WrapPanel Orientation="Vertical" Width="2000" />
<!--<VariableSizedWrapGrid Margin="0,0,80,0" ItemHeight="1" ItemWidth="1" />-->
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,80,0" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="{Binding RowSpan}" Width="{Binding ColSpan}">
<Grid.Background>
<SolidColorBrush Color="{Binding Color}" />
</Grid.Background>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</Grid>
但有一个问题。我没有正确设置 WrapPanel 的宽度。在上面的代码中,我将其默认为 2000(这很荒谬)。您必须弄清楚那部分并在此处发布您的解决方案。 (我不能做所有事情)否则,这就是你要求的 100%。
WinRT WrapPanel 在我的博客http://jerrynixon.com(在左栏)
祝你好运,法尔汉。
另见:ms forum where you asked the same thing