简体   繁体   English

如何在UWP中创建具有固定列数的网格视图?

[英]How to a create grid view with a fixed number of columns in UWP?

I'm trying to create a calendar component for my app. 我正在尝试为我的应用创建日历组件。 Currently I'm working on a calendar view that is meant to show 7 days wide month, where each day is a list of events. 目前,我正在使用日历视图,该视图旨在显示一个月宽7天的时间,其中每天是事件列表。

To do so I need to inform GridView that I always want it to be formatted in a way to hold 7 elements in each row without gaps between them. 为此,我需要通知GridView ,我一直希望将其格式化为在每行中容纳7个元素,并且它们之间没有间隙。 The underlying collection would always contain multiple of seven elements. 基础集合将始终包含七个元素中的多个。

What I currently have ( ExtendedGridView / ExtendedListView are the same basic GridView / ListView and do not do any visual transformations): 我目前拥有的( ExtendedGridView / ExtendedListView是相同的基本GridView / ListView并且不进行任何可视化转换):

<local:ExtendedGridView  x:Name="gridView" Margin="0"
                             IsSwipeEnabled="True"                                     
                             IsItemClickEnabled="True"
                             SelectionMode="Single"
                             ItemsSource="{Binding GroupCollection}"
                             VerticalAlignment="Stretch"
                             HorizontalAlignment="Stretch">

        <local:ExtendedGridView.DataContext>
            <local:MainViewModel/>
        </local:ExtendedGridView.DataContext>
        <local:ExtendedGridView.Resources>                
            <DataTemplate x:Key="DefaultGridItemTemplate">
                <Grid Width="100*" Height="60*" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <local:ExtendedListView Margin="0"
                                            IsSwipeEnabled="False"
                                            IsItemClickEnabled="True"
                                            ItemsSource="{Binding Group}">
                        <local:ExtendedListView.ItemContainerStyle>
                            <Style
                                TargetType="ListViewItem">
                                <Setter
                                    Property="HorizontalAlignment"
                                    Value="Stretch" />
                                <Setter
                                    Property="VerticalAlignment"
                                    Value="Stretch" />
                            </Style>
                        </local:ExtendedListView.ItemContainerStyle>

                        <local:ExtendedListView.Resources>
                            <DataTemplate x:Key="DefaultInnerListItemTemplate">
                                <TextBlock Text="{Binding Data}" 
                                               FontSize="12" Foreground="Green"/>
                            </DataTemplate>
                        </local:ExtendedListView.Resources>
                        <local:ExtendedListView.ItemTemplate>
                            <Binding Source="{StaticResource DefaultInnerListItemTemplate}"/>
                        </local:ExtendedListView.ItemTemplate>
                    </local:ExtendedListView>
                </Grid>
            </DataTemplate>

            <ItemsPanelTemplate x:Key="DefaultGridPanelTemplate">
                <WrapGrid MaximumRowsOrColumns="7" Orientation="Horizontal" HorizontalChildrenAlignment="Stretch" VerticalChildrenAlignment="Stretch">
                </WrapGrid>
            </ItemsPanelTemplate>
        </local:ExtendedGridView.Resources>
        <local:ExtendedGridView.ItemContainerStyle>
            <Style
                TargetType="GridViewItem">
                <Setter
                    Property="HorizontalAlignment"
                    Value="Stretch" />
                <Setter
                    Property="VerticalAlignment"
                    Value="Stretch" />
            </Style>
        </local:ExtendedGridView.ItemContainerStyle>
        <local:ExtendedGridView.ItemTemplate>
            <Binding Source="{StaticResource DefaultGridItemTemplate}"/>
        </local:ExtendedGridView.ItemTemplate>
        <local:ExtendedGridView.ItemsPanel>
            <Binding Source="{StaticResource DefaultGridPanelTemplate}"/>
        </local:ExtendedGridView.ItemsPanel>
    </local:ExtendedGridView>

How it currently looks: 当前外观:

GridView屏幕截图

I'm fairly new to using XAML so maybe I should take a completely different route. 我对使用XAML相当陌生,所以也许我应该采取完全不同的方法。 Any help and advices are appreciated. 任何帮助和建议,不胜感激。

It would be great if you can create multiple visual state for different screen where you need to display this gridview and fix columns width accordingly. 如果您可以在需要显示此gridview并相应地固定列宽的不同屏幕上创建多个视觉状态,那将是很好的。 you can follow this link 您可以点击此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM