在gridview中选择组样式

alb*_*bux 5 c# microsoft-metro windows-8 windows-runtime

我正在为Windows 8开发一个app metro.我使用GridApp(xaml)项目,但我想在每个部分使用不同的组样式.

我的代码是:

public class GroupTemplateSelector : GroupStyleSelector
{

    public GroupStyle NewsItemGroupStyle { 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

        if (level == 3)
        {
            return NewsItemGroupStyle;
        }
        else
        {
            return NormalGroupStyle;
        }

        throw new ArgumentException("Unexpected group type"); 

    }
}
Run Code Online (Sandbox Code Playgroud)

我使用这个类来选择组样式和XAML

<!-- NewsItemGroupStyle -->
<GroupStyle x:Key="NewsItemGroupStyle">
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
        </DataTemplate>
    </GroupStyle.HeaderTemplate>
    <GroupStyle.Panel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" Margin="0,0,80,0" VerticalAlignment="Bottom"/>
        </ItemsPanelTemplate>
    </GroupStyle.Panel>
</GroupStyle>


<!-- NormalItemGroupStyle -->
<GroupStyle x:Key="NormalGroupStyle">
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
            <Grid Margin="1,0,0,6">
                <Button
                    AutomationProperties.Name="Group Title"
                    Content="{Binding Title}"
                    Background="Blue"
                    Click="Header_Click"
                    Style="{StaticResource TextButtonStyle}"
                    />
            </Grid>
        </DataTemplate>
    </GroupStyle.HeaderTemplate>
    <GroupStyle.Panel>
        <ItemsPanelTemplate>
            <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
        </ItemsPanelTemplate>
    </GroupStyle.Panel>
</GroupStyle>

<!-- selector -->
<common:GroupTemplateSelector 
    x:Key="groupSelector"
    NewsItemGroupStyle="{StaticResource NewsItemGroupStyle}"
    NormalGroupStyle="{StaticResource NormalGroupStyle}" />
Run Code Online (Sandbox Code Playgroud)

但是样式组会立即更改.

Jar*_*SFT 0

正如 Lvsti 指出的,GroupStyleSelector 只能更改每个级别的样式的样式。例如,所有 0 级组将具有相同的样式,但所有 1 级组可以具有不同的样式。目前不可能在 0 级拥有两个具有不同风格的不同组。事实上,为同一级别的任何组返回的最后一个样式似乎会应用于该级别的所有组。不幸的是,但这就是当前的设计。

开发支持、设计支持以及更多精彩内容即将推出:http://bit.ly/winappsupport