gpt4 book ai didi

c# - 如何让 Metro GridView 中的组使用不同的布局?

转载 作者:可可西里 更新时间:2023-11-01 08:29:37 24 4
gpt4 key购买 nike

我正在编写 Windows 8 Metro 应用程序。我正在尝试绘制一个包含三个组的 GridView。我希望其中一个小组以不同于其他小组的方式布置他们的项目。我以前在 WPF 中使用过选择器,所以我认为这是一条很好的路线。所以我尝试了 GroupStyleSelector 并找到了这个 example on MSDN :

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您可以通过将对象强制转换为 ICollectionView 并访问 .Group 属性来提取对象,您将在其中获取将组绑定(bind)到的对象。这允许对模板进行智能决策。但是它仍然对我(或线程中的其他人)不起作用,因为尽管返回了不同的样式,但只应用了一种样式。

编辑:事实证明 GroupTemplate 并非旨在生成不同的组。它旨在更改组的 View ,例如在快照 View 或所有组都更改的类似情况下。

关于c# - 如何让 Metro GridView 中的组使用不同的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11418511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com