xaml - 将 GridView (XAML) 组 'float: left' 中的项目包装/ float 在最上面的项目上

标签 xaml windows-8

如何根据下图编写在 GridView (XAML-Win8) 组中流动项目的代码?

我目前有一个自定义 TemplateSelector为第一项选择不同的(更大的)模板,但是这里指定的流程:

<GroupStyle.Panel>
    <ItemsPanelTemplate>
        <q42:WrapPanel Orientation="Horizontal" Width="440" Margin="0,0,80,0"/>
        <!-- also tried VariableSizedWrapGrid -->
    </ItemsPanelTemplate>
</GroupStyle.Panel>

类似地包装项目 1 到 3,但随后将项目 4 放在项目 6 的位置,而不填写项目 4 和 5。

问题变成;我如何编写类似于 css 的代码:
.item  { display: inline-block; }
.item1 { float: left; }

,这将使项目像我想要的那样流动?

What I'd like the flow to look like

最佳答案

Andreas Hammar 将我与一个可行的解决方案联系起来:

What it looks like

using System.Collections.Generic;
using Application1.Data;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Application1
{
    public class MyGridView : GridView
    {
        int _rowVal;
        int _colVal;
        readonly List<Size> _sequence;

        public MyGridView()
        {
            _sequence = new List<Size>
                {
                    LayoutSizes.PrimaryItem,
                    LayoutSizes.SecondarySmallItem,
                    LayoutSizes.SecondarySmallItem,
                    LayoutSizes.OtherSmallItem,
                    LayoutSizes.OtherSmallItem, // 5 
                    LayoutSizes.OtherSmallItem,
                    LayoutSizes.SecondaryTallItem, // 7
                    LayoutSizes.OtherSmallItem,
                    LayoutSizes.SecondarySmallItem, // 9
                    LayoutSizes.OtherSmallItem,
                    LayoutSizes.SecondarySmallItem, // 11
                    LayoutSizes.SecondarySmallItem,
                    LayoutSizes.OtherSmallItem,
                    LayoutSizes.OtherSmallItem
                };
        }

        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
            var dataItem = item as SampleDataItem;
            var index = -1;

            if (dataItem != null)
            {
                index = dataItem.Group.Items.IndexOf(dataItem);
            }

            if (index >= 0 && index < _sequence.Count)
            {
                _colVal = (int) _sequence[index].Width;
                _rowVal = (int) _sequence[index].Height;
            }
            else
            {
                _colVal = (int) LayoutSizes.OtherSmallItem.Width;
                _rowVal = (int) LayoutSizes.OtherSmallItem.Height;
            }

            VariableSizedWrapGrid.SetRowSpan(element as UIElement, _rowVal);
            VariableSizedWrapGrid.SetColumnSpan(element as UIElement, _colVal);
        }
    }

    public static class LayoutSizes
    {
        public static Size PrimaryItem = new Size(6, 2);
        public static Size SecondarySmallItem = new Size(3, 1);
        public static Size SecondaryTallItem = new Size(2, 2);
        public static Size OtherSmallItem = new Size(2, 1);
    }
}
    <local:MyGridView.ItemsPanel>
        <ItemsPanelTemplate>                        
            <VirtualizingStackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </local:MyGridView.ItemsPanel>
    <local:MyGridView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <Grid Margin="1,0,0,6">
                        <Button
                            AutomationProperties.Name="Group Title"
                            Content="{Binding Title}"
                            Click="Header_Click"
                            Style="{StaticResource TextButtonStyle}"/>
                    </Grid>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
            <GroupStyle.Panel>
                <ItemsPanelTemplate>
                    <VariableSizedWrapGrid ItemWidth="80" ItemHeight="160" Orientation="Vertical" Margin="0,0,80,0" MaximumRowsOrColumns="3"/>
                </ItemsPanelTemplate>
            </GroupStyle.Panel>
        </GroupStyle>
    </local:MyGridView.GroupStyle>
</local:MyGridView>

关于xaml - 将 GridView (XAML) 组 'float: left' 中的项目包装/ float 在最上面的项目上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12785954/

相关文章:

c# - wpf 组合框默认从 itemssource 选择

c# - 如何在WPF中显示一个从右向左滑动效果的窗口

.net - 如何使用 PRISM 创建 XAML 启动屏幕

c++ - 如何在 Windows 8 上使用 C++ 将 .jpg 转换为 .png?

c# - 无法在 Windows 应用商店应用程序中使用 Trace.WriteLine

c++ - SystemParametersInfo 和 ERROR_OPERATION_IN_PROGRESS

c# - 如何从 XAML 内部访问嵌套命名空间?

c# - 命名空间 "X"中不存在名称 "using:Y"

c# - 使用 C#/XAML 在 Metro 风格应用程序中将 Canvas 保存为图像

windows-8 - 我应该如何为 CredentialPickerOptions.PreviousCredential 保留 CredentialPickerResults.Credential?