Ger*_*hes 12 .net c# wpf xaml windows-runtime
我正在使用Flipview和DataTemplateSelector在运行时确定要应用于我的控件中显示项目的DataTemplate.
我有两个DataTemplate,一个是静态的,第二个可以由未确定数量的项使用.
目前
我的第一个视图显示: - "这是一个测试 - 内容"
Followed by 18 other views看起来像这样: - " http://www.google.com/ 0" - " http://www.google.com/ 1" - " http://www.google.com/ 2" - 等等直到17日
我想要
要将" http://www.google.com/ " 项目分组为3 on a 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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想也许这可以用组和列表视图来实现.但我不确定如何做到这一点.
可能这是一个愚蠢的问题,但使用谷歌,我找不到答案.英语也不是我的母语; 请原谅输入错误.
我认为实现您正在寻找的内容的方法是以更好地代表您想要显示的内容的方式公开数据。然后,您可以使用嵌套控件来显示它。我只是把它们放在一起(使用我自己的测试数据)。它可能不完全是您想要的,但它应该可以帮助您解决问题。
ViewModel
这里我创建了一个辅助方法来构建带有子集合的集合,每个子集合有 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;
}
}
Run Code Online (Sandbox Code Playgroud)
Xaml
这里我使用 ItemsControl 垂直堆叠数据中的项目。每个项目要么是三个对象的列表,要么是单个对象。我FlipView对三个对象的每个列表使用 a ,ContentPresenter对单个对象使用 simple 。
<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}" />
Run Code Online (Sandbox Code Playgroud)
注意:您通常不需要这样的模板选择器,但由于您需要在 aList<T>和 an之间进行选择Object,因此我知道无法仅使用DataTemplate.TargetTypeXaml 中的属性来识别差异,因为List<t>它是泛型类型。(我尝试过{x:Type collections:List`1},但没有成功。)