c# - 在 WinRT 中使用 FlipView 和 DataTemplateSelector 动态显示项目

标签 c# .net wpf xaml windows-runtime

我正在使用 Flipview 和 DataTemplateSelector 在运行时确定应用哪个 DataTemplate 来显示我的控件中的项目。

我有两个 DataTemplate,一个是静态的,第二个可以由不确定数量的项目使用。

目前

我的第一个 View 显示: - "这是一个测试 - 内容"

后面是 18 个其他 View ,如下所示: - “http://www.google.com/ 0” - “http://www.google.com/ 1” - “http://www.google.com/ 2” - 以此类推直到 17

我要

项“http://www.google.com/”在 View 中被分组为 3

例如第二个 View 将显示:

第三个 View 将显示:

等等..

下面是我的代码:

FlipViewDemo.xaml

<Page.Resources>
    <DataTemplate x:Key="FirstDataTemplate">
        <Grid>
            <TextBlock Text="{Binding Content}" Margin="10,0,18,18"></TextBlock>
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="SecondDataTemplate">
        <TextBox Text="{Binding Url}"></TextBox>
    </DataTemplate>
    <local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
                FirstTextTemplate="{StaticResource FirstDataTemplate}"
                SecondTextTemplate="{StaticResource SecondDataTemplate}">
    </local:MyDataTemplateSelector>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <FlipView x:Name="itemGridView" ItemTemplateSelector="{StaticResource MyDataTemplateSelector}" 
    Margin="265,220,284,162">
    </FlipView>
</Grid>

FlipViewDemo.xaml.cs

public sealed partial class FlipViewDemo : Page
{
    public FlipViewDemo()
    {
        this.InitializeComponent();

        var items = new List<BaseClass>();

        items.Add(new FirstItem
        {
            Content="This is a test - Content"
        });

        for (int i = 0; i < 18; i++)
        {
            items.Add(new SecondItem
            {
                Url = "http://www.google.com/ " + i.ToString() 
            });
        }
        itemGridView.ItemsSource = items;
    }
}
public class BaseClass
{
}

public class FirstItem : BaseClass
{
    public string Content { get; set; }
}

public class SecondItem : BaseClass
{
    public string Url { get; set; }
}

public class MyDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate FirstTextTemplate { get; set; }
    public DataTemplate SecondTextTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item,
                                                       DependencyObject container)
    {
        if (item is FirstItem)
            return FirstTextTemplate;
        if (item is SecondItem)
            return SecondTextTemplate;

        return base.SelectTemplateCore(item, container);
    }
}

我在想也许这可以通过组和 ListView 来实现。但我不确定如何做到这一点。

这可能是一个愚蠢的问题,但使用 Google,我找不到答案。英语也不是我的母语;请原谅打字错误。

最佳答案

我认为实现您正在寻找的方法是以更好地代表您想要显示的内容的方式公开数据。然后,您可以使用嵌套控件来显示它。我只是把它放在一起(使用我自己的测试数据)。它可能不是您想要的,但它应该可以帮助您解决问题。

View 模型
在这里,我创建了一个辅助方法来构建包含子集合的集合,每个子集合都有 3 个项目。

class FlipViewDemo
{
    private List<object> mData;

    public IEnumerable<object> Data
    {
        get { return mData; }
    }

    public FlipViewDemo()
    {
        mData = new List<object>();
        mData.Add("Test String");
        for (int i = 0; i < 18; ++i)
        {
            AddData("Test Data " + i.ToString());
        }
    }

    private void AddData(object data)
    {
        List<object> current = mData.LastOrDefault() as List<object>;
        if (current == null || current.Count == 3)
        {
            current = new List<object>();
            mData.Add(current);
        }
        current.Add(data);
    }
}

class TemplateSelector : DataTemplateSelector
{
    public DataTemplate ListTemplate { get; set; }
    public DataTemplate ObjectTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item is List<object>) return ListTemplate;
        return ObjectTemplate;
    }
}

Xaml
在这里,我使用 ItemsControl 垂直堆叠数据中的项目。每个项目要么是三个对象的列表,要么是单个对象。我用 FlipView对于三个对象的列表和一个简单的 ContentPresenter对于单个对象。

<Page.Resources>
    <DataTemplate x:Key="ListTemplate">
        <FlipView
            ItemsSource="{Binding}">
            <FlipView.ItemTemplate>
                <DataTemplate>
                    <ContentPresenter
                        Margin="0 0 10 0"
                        Content="{Binding}" />
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>
    </DataTemplate>
    <DataTemplate x:Key="ObjectTemplate">
        <ContentPresenter
            Margin="0 0 10 0"
            Content="{Binding}" />
    </DataTemplate>
    <local:TemplateSelector
        x:Key="TemplateSelector"
        ListTemplate="{StaticResource ListTemplate}"
        ObjectTemplate="{StaticResource ObjectTemplate}" />
</Page.Resources>

<ItemsControl
    ItemsSource="{Binding Data}"
    ItemTemplateSelector="{StaticResource TemplateSelector}" />

注意:对于这样的事情,您通常不需要模板选择器,但由于您需要在 List<T> 之间进行选择和一个 Object , 据我所知,仅使用 DataTemplate.TargetType 无法识别差异由于 List<t> 而来自 Xaml 的属性作为通用类型。 (我试过 {x:Type collections:List`1} 但没有成功。)

关于c# - 在 WinRT 中使用 FlipView 和 DataTemplateSelector 动态显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30397863/

相关文章:

c# - 为什么wpf控件只有一个命令? (MVVM)

c# - 如何在不使用指针的情况下模拟指针?

c# - 如何从sql数据库中检索excel文件

c# - 新建具有非常大初始容量的 List<T> 并填充它与简单地填充 LinkedList<T> 的性能

c# - MVVM 疯狂 : Commands

c# - 如何通过同一 View 模型的属性传递事件

c# - 我可以获取 .ascx 文件以在类库中编译吗?

c# - C#命令行脚本在Visual Studio中过早结束

.net - 创建 MSI 的最佳工具

c# - SQLite 数据库与 C# 执行速度非常慢