我正在编写一个 Windows 8 Metro 应用程序。我正在尝试绘制一个包含三个组的 GridView。我希望其中一组的项目布局与其他组不同。我以前在 WPF 中使用过选择器,所以我认为这是一个很好的途径。所以我尝试了 GroupStyleSelector 并发现了这个MSDN 上的示例 http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh780627.aspx:
public class ListGroupStyleSelector : GroupStyleSelector
{
protected override GroupStyle SelectGroupStyleCore(object group, uint level)
{
return (GroupStyle)App.Current.Resources["listViewGroupStyle"];
}
}
所以我根据适合我的内容对其进行了更改/扩展:
CS:
public class ExampleListGroupStyleSelector : GroupStyleSelector
{
public ExampleListGroupStyleSelector ()
{
OneBigItemGroupStyle = null;
NormalGroupStyle = null;
}
public GroupStyle OneBigItemGroupStyle { get; set; }
public GroupStyle NormalGroupStyle { get; set; }
protected override GroupStyle SelectGroupStyleCore( object group, uint level )
{
// a method that tries to grab an enum off the bound data object
var exampleListType= GetExampleListType( group );
if ( exampleListType== ExampleListType.A)
{
return OneBigItemGroupStyle;
}
if ( exampleListType== ExampleListType.B|| exampleListType== ExampleListType.B)
{
return NormalGroupStyle;
}
throw new ArgumentException( "Unexpected group type" );
}
}
XAML:
<Page.Resources>
<ExampleListGroupStyleSelector
x:Key="ExampleListGroupStyleSelector"
OneBigItemGroupStyle="{StaticResource OneBigGroupStyle}"
NormalGroupStyle="{StaticResource NormalGroupStyle}" />
</Page.Resources>
<GridView
ItemsSource="{Binding Source={StaticResource exampleListsViewSource}}"
GroupStyleSelector="{StaticResource ExampleListGroupStyleSelector}">
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel
Orientation="Horizontal" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
但是我在选择器中给出的组是 null 或 DependencyObject,我似乎无法获取任何数据。如果没有给我任何信息,我该如何做出关于如何更改 GroupStyle 的明智决定。有没有办法可以通过附加属性或类似的方式传递属性?
基于本论坛thread http://social.msdn.microsoft.com/Forums/hr/winappswithcsharp/thread/5f12273f-e000-4c96-a4bc-6ccc18a104a0?prof=required您可以通过将对象转换为 ICollectionView 并访问 .Group 属性来提取该对象,您将在其中获取将组绑定到的对象。这允许对模板进行智能决策。然而,它仍然对我(或线程中的其他人)不起作用,因为尽管返回了不同的样式,但只应用了一种样式。
编辑:事实证明 GroupTemplate 并不旨在生成不同的组。其目的是更改组的视图,例如在快照视图或所有组都发生更改的类似情况下。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)