【问题标题】:Multiple item template inside grid view in Windows 8.1 store appWindows 8.1 商店应用程序中的网格视图内的多个项目模板
【发布时间】:2015-03-11 04:51:32
【问题描述】:

在 gridview 项目模板中,是否可以有多个模板?在我的屏幕中,我在 GridView 控件的单个部分中有一组图标。我希望其中一个图标具有更大的尺寸。我怎样才能实现它。

<GridView>
<GridView.ItemTemplate>
<DataTemplate>
<Grid/>
<DataTemplate/>
<GridView.ItemTemplate/>
<GridView/>

在此模板中,网格的尺寸对于所有元素将保持不变。我如何自定义它,以便其中一个项目具有不同尺寸的网格。

【问题讨论】:

    标签: c# xaml windows-store-apps winrt-xaml windows-8.1


    【解决方案1】:

    是的,你可以。你需要一个DataTemplateSelector。您可以为网格/列表中的选定对象选择适当的模板。

    选择器示例:

    class TemplateSelectorMyPage : DataTemplateSelector
        {
            public DataTemplate NormalIconTemplate { get; set; }
            public DataTemplate BigIconTemplate { get; set; }
    
            protected override DataTemplate SelectTemplateCore( object item, DependencyObject container )
            {
                var uiElement = container as UIElement;
                if( uiElement == null )
                {
                    return base.SelectTemplateCore( item, container );
                }
    
                if ( item is BigIconItem )
                {
                    return BigIconTemplate;
                }
                else if ( item is NormalIconItem )
                {
                    return NormalIconTemplate;
                }
                return base.SelectTemplateCore( item, container );
            }
    

    在 XAML 中:

    <Page.Resources>
        <local:TemplateSelectorMyPage  x:Key="mySelector"
            BigIconTemplate="{StaticResource myBigIconTemplate}"
            NormalIconTemplate="{StaticResource myNormalIconTemplate}">
        </local:TemplateSelectorBusinessPage>
    </Page.Resources>
    

    在网格 XAML 中:

    <GridView           
                x:Name="itemGridView"
                ItemTemplateSelector="{StaticResource mySelector}"
    

    【讨论】:

    • 谢谢。如何在对 gridview 控件进行数据绑定时设置模板?
    • if ( item is BigIconItem ) 之后的 TemplateSelectorMyPage 中,您可以随意更改行为,以选择合适的模板。模板保存在DataTemplate 属性中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多