Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SemanticZoom中的UWP GridView ItemTemplateSelector未应用_C#_Xaml_Gridview_Uwp_Datatemplateselector - Fatal编程技术网

C# SemanticZoom中的UWP GridView ItemTemplateSelector未应用

C# SemanticZoom中的UWP GridView ItemTemplateSelector未应用,c#,xaml,gridview,uwp,datatemplateselector,C#,Xaml,Gridview,Uwp,Datatemplateselector,我有一个GridView作为SemanticZoom控件中的缩小视图。此GridView使用自定义DataTemplateSelector作为ItemTemplateSelector。DataTemplateSelector返回具有不同前景颜色的DataTemplate,具体取决于组中是否包含任何项 但是,即使DataTemplateSelector似乎可以工作并返回正确的模板,GridView也只使用了一个模板,并且文本的颜色都相同 以下是GroupedListView的XAML: <U

我有一个GridView作为SemanticZoom控件中的缩小视图。此GridView使用自定义DataTemplateSelector作为ItemTemplateSelector。DataTemplateSelector返回具有不同前景颜色的DataTemplate,具体取决于组中是否包含任何项

但是,即使DataTemplateSelector似乎可以工作并返回正确的模板,GridView也只使用了一个模板,并且文本的颜色都相同

以下是GroupedListView的XAML:

<UserControl
    x:Class="GroupList.GroupList.GroupedListView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:GroupList.GroupList"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:GroupList.Model"
    xmlns:wuxdata="using:Windows.UI.Xaml.Data"
    mc:Ignorable="d">

    <UserControl.Resources>
        <!-- Use a collection view source for content that presents itself as a list of items that can be grouped or sorted.  Otherwise, you can use x:Bind
        directly on the ListView's item source to for further optimization. Please see the AppUIBasics sample for an example of how to do this.  -->
        <CollectionViewSource x:Name="ContactsCVS"  IsSourceGrouped="True" />

        <Style TargetType="TextBlock" x:Key="TileHeaderTextStyle" BasedOn="{StaticResource ProximaNovaSemiBold}">
            <Setter Property="FontSize" Value="54" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="FontWeight" Value="ExtraBold"/>
            <Setter Property="FontStretch" Value="Expanded" />
        </Style>

        <Style TargetType="TextBlock" x:Key="TileHeaderTextStyleGray" BasedOn="{StaticResource TileHeaderTextStyle}">
            <Setter Property="Foreground" Value="Khaki" />
        </Style>

        <!-- When using x:Bind, you need to set x:DataType -->
        <DataTemplate x:Name="ContactListViewTemplate" x:DataType="data:Contact">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Ellipse x:Name="Ellipse"
                         Grid.RowSpan="2"
                         Width ="32"
                         Height="32"
                         Margin="6"
                         VerticalAlignment="Center"
                         HorizontalAlignment="Center"
                         Fill="LightGray"/>
                <TextBlock Grid.Column="1"
                           Text="{x:Bind Name}" 
                           x:Phase="1"  
                           Style="{ThemeResource BaseTextBlockStyle}"
                           Margin="12,6,0,0"/>
                <TextBlock  Grid.Column="1"
                            Grid.Row="1"
                            Text="{x:Bind Position}" 
                            x:Phase="2"
                            Style="{ThemeResource BodyTextBlockStyle}"
                            Margin="12,0,0,6"/>
            </Grid>
        </DataTemplate>
        <DataTemplate x:Key="GrayZoomedOutTemplate" x:DataType="wuxdata:ICollectionViewGroup">
            <TextBlock Text="{x:Bind Group.(data:GroupInfoList.Key)}" Margin="0,0,0,5" Style="{StaticResource TileHeaderTextStyleGray}" />
        </DataTemplate>
        <DataTemplate x:Key="ZoomedOutTemplate" x:DataType="wuxdata:ICollectionViewGroup">
            <TextBlock Text="{x:Bind Group.(data:GroupInfoList.Key)}" Margin="0,0,0,5" Style="{StaticResource TileHeaderTextStyle}" />
        </DataTemplate>
        <local:GroupEmptyOrFullSelector x:Key="GroupEmptyOrFullSelector" Empty="{StaticResource GrayZoomedOutTemplate}" Full="{StaticResource ZoomedOutTemplate}" />
    </UserControl.Resources>
    <!--#region Navigation Panel -->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Vertical">
            <TextBlock Margin="15,0,0,0" Text="Paula's SemanticZoom Sandbox" Grid.Row="0"
                           VerticalAlignment="Center"
                           Style="{ThemeResource TitleTextBlockStyle}" />
            <Button x:Name="ZoomInOutBtn" Content="ABC↕" Click="ZoomInOutBtn_Click" Width="60" HorizontalAlignment="Center" BorderThickness="0" />
        </StackPanel>
        <!--#endregion-->
        <SemanticZoom x:Name="ZoomControl" Grid.Row="1">
            <SemanticZoom.ZoomedInView>
                <GridView ItemsSource="{x:Bind ContactsCVS.View}"
                          ItemTemplate="{StaticResource ContactListViewTemplate}"
                          SelectionMode="Single"
                          ShowsScrollingPlaceholders="True">

                    <GridView.GroupStyle>
                        <GroupStyle HidesIfEmpty="True">
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate x:DataType="data:GroupInfoList">
                                    <TextBlock Text="{x:Bind Key}" 
                                               Style="{ThemeResource TitleTextBlockStyle}" />
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                        </GroupStyle>
                    </GridView.GroupStyle>
                </GridView>
            </SemanticZoom.ZoomedInView>
            <SemanticZoom.ZoomedOutView>
                <GridView ItemTemplateSelector="{StaticResource GroupEmptyOrFullSelector}" ScrollViewer.VerticalScrollBarVisibility="Disabled"  Margin="0, 200" Width="475" ItemsSource="{x:Bind ContactsCVS.View.CollectionGroups}" SelectionMode="None" >
                </GridView>
            </SemanticZoom.ZoomedOutView>
        </SemanticZoom>
    </Grid>
</UserControl>

这是DataTemplateSelector:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.Foundation.Collections;
using GroupList.Model;

namespace GroupList.GroupList
{
    /// <summary>
    /// This determines whether or not the Group passed during binding is empty or not and allows selection
    /// of the proper DataTemplate based on this value.
    /// </summary>
    class GroupEmptyOrFullSelector : DataTemplateSelector
    {
        private DataTemplate _full;
        private DataTemplate _empty;
        public DataTemplate Full
        {
            set { _full = value; }
            get { return _full; }
        }
        public DataTemplate Empty
        {
            set { _empty = value; }
            get { return _empty; }
        }


        protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
        {

            var itemType = item.GetType();
            var isGroup = itemType.Name == "GroupInfoList";
            bool isEmpty = false;
            GroupInfoList groupItem;

            if (isGroup)
            {
                groupItem = item as GroupInfoList;
                isEmpty = groupItem.Count == 0;
            }

            // Disable empty items
            var selectorItem = container as SelectorItem;
            if (selectorItem != null)
            {
                selectorItem.IsEnabled = !isEmpty;
            }

            if (isEmpty)
            {
                return Empty;
            }
            else
            {
                return Full;
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Windows.UI.Xaml;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Controls.Primitives;
使用Windows。
使用GroupList.Model;
名称空间GroupList.GroupList
{
/// 
///这将确定绑定期间传递的组是否为空,并允许选择
///基于此值的正确DataTemplate的。
/// 
类GroupEmptyOrFullSelector:DataTemplateSelector
{
私有数据模板_full;
私有数据模板_为空;
公共数据模板已满
{
设置{u full=value;}
获取{return\u full;}
}
公共数据模板为空
{
设置{u empty=value;}
获取{return\u empty;}
}
受保护的覆盖数据模板SelectTemplateCore(对象项,DependencyObject容器)
{
var itemType=item.GetType();
var isGroup=itemType.Name==“GroupInfoList”;
bool isEmpty=false;
GroupInfoList-groupItem;
if(isGroup)
{
groupItem=作为GroupInfoList的项目;
isEmpty=groupItem.Count==0;
}
//禁用空项
var selectorItem=容器作为selectorItem;
如果(selectorItem!=null)
{
selectorItem.IsEnabled=!isEmpty;
}
如果(我是空的)
{
返回空;
}
其他的
{
全额退还;
}
}
}
}

所以,问题出在这里:

在DataTemplateSelector列表中,即使传递的“对象项”不是组,它也取决于模板选择逻辑。有时,传递给函数的“对象”不是组对象,而是一般依赖对象。在这种情况下,模板选择逻辑需要返回一个默认模板,仅当“对象项”是组对象时才返回一个特定模板。因此,新函数如下所示:

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {

        var itemType = item.GetType();
        var isGroup = itemType.Name == "GroupInfoList";
        bool isEmpty = false;
        GroupInfoList groupItem;

        if (isGroup)
        {
            groupItem = item as GroupInfoList;
            isEmpty = groupItem.Count == 0;

            // Disable empty items
            var selectorItem = container as SelectorItem;
            if (selectorItem != null)
            {
                selectorItem.IsEnabled = !isEmpty;
            }

            if (isEmpty)
            {
                return Empty;
            }
            else
            {
                return Full;
            }

        }

        return Full;

    }

您的代码看起来是正确的。你查过你的日期来源了吗?您确定有空组吗?是的,有空组,我坐在调试器中检查了每个组。我现在可以重现您的问题。这个答案也不起作用。