none
GroupStyleSelector无效疑惑? RRS feed

  • 问题

  • Hello:

    我尝试使用GroupStyleSelector使得我的每个标题HeaderTemplate有不同的效果:

    sample如下:

        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
    
    
                ObservableCollection<Group> groups = new ObservableCollection<Group>()
                {
                    new Group(){Title="Group 1",GroupStyle =1,
                                Items=new ObservableCollection<Item>()
                                {
                                    new Item (){ContainerStyle=1},
                                    new Item (){ContainerStyle=1},
                                    new Item (){ContainerStyle=1},
                                    new Item (){ContainerStyle=1},
                                    new Item (){ContainerStyle=1},
                                    new Item (){ContainerStyle=1},
                                    new Item (){ContainerStyle=1},
                                    new Item (){ContainerStyle=1}
                                }
                    },
                    new Group(){Title="Group 2",GroupStyle =2,
                                Items=new ObservableCollection<Item>()
                                {
                                    new Item (){ContainerStyle=2},
                                    new Item (){ContainerStyle=2},
                                    new Item (){ContainerStyle=2},
                                    new Item (){ContainerStyle=2}
                                }
                    },
                    new Group(){Title="Group 3",GroupStyle =3,
                                Items=new ObservableCollection<Item>()
                                {
                                    new Item (){ContainerStyle=3},
                                    new Item (){ContainerStyle=3},
                                    new Item (){ContainerStyle=3},
                                    new Item (){ContainerStyle=3}
                                }
                    },
                    new Group(){Title="Group 4",GroupStyle =4,
                                Items=new ObservableCollection<Item>()
                                {
                                    new Item (){ContainerStyle=4},
                                    new Item (){ContainerStyle=4},
                                    new Item (){ContainerStyle=4},
                                    new Item (){ContainerStyle=4}
                                }
                    }
                };
    
                this.DataContext = groups;
            }
    
        }
    
        public class MyStyleSelector : StyleSelector
        {
            public Style style1x1 { get; set; }
            public Style style1x2 { get; set; }
            public Style style2x1 { get; set; }
            public Style style2x2 { get; set; }
    
            protected override Style SelectStyleCore(object item, DependencyObject container)
            {
                if (item != null && item is Item)
                {
                    Item i = item as Item;
                    switch (i.ContainerStyle)
                    {
                        case 1: return style1x1;
                        case 2: return style1x2;
                        case 3: return style2x1;
                        case 4: return style2x2;
                    }
                }
                return base.SelectStyleCore(item, container);
            }
        }
    
        public class MyGroupStyleSelector : GroupStyleSelector
        {
            public GroupStyle groupStyle1 { get; set; }
            public GroupStyle groupStyle2 { get; set; }
            public GroupStyle groupStyle3 { get; set; }
            public GroupStyle groupStyle4 { get; set; }
    
            protected override GroupStyle SelectGroupStyleCore(object group, uint level)
            {
                var collectionViewGroup = group as ICollectionViewGroup;
                if (collectionViewGroup != null)
                {
                    Group g = collectionViewGroup.Group as Group;
                    if (g is Group)
                    {
                        switch (g.GroupStyle)
                        {
                            case 1: return groupStyle1;
                            case 2: return groupStyle2;
                            case 3: return groupStyle3;
                            case 4: return groupStyle4;
                        }
                    }
                }
                return null;
            }
        }
    
    
        public class Item
        {
            public int ContainerStyle { get; set; }
        }
    
        public class Group
        {
            public int GroupStyle { get; set; }
    
            public string Title { get; set; }
            public ObservableCollection<Item> Items { get; set; }
        }
    
    
    
    
    
    
    
    <Page
        x:Class="App13.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App13"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
        <Page.Resources>
            <Style x:Key="style1x1" TargetType="GridViewItem">
                <Setter Property="VariableSizedWrapGrid.ColumnSpan" Value="1"/>
                <Setter Property="VariableSizedWrapGrid.RowSpan" Value="1"/>
                <Setter Property="Width" Value="100"/>
                <Setter Property="Height" Value="100"/>
                <Setter Property="Background" Value="Red"/>
            </Style>
            <Style x:Key="style2x2" TargetType="GridViewItem">
                <Setter Property="VariableSizedWrapGrid.ColumnSpan" Value="2"/>
                <Setter Property="VariableSizedWrapGrid.RowSpan" Value="2"/>
                <Setter Property="Width" Value="200"/>
                <Setter Property="Height" Value="200"/>
                <Setter Property="Background" Value="Green"/>
            </Style>
            <Style x:Key="style2x1" TargetType="GridViewItem">
                <Setter Property="VariableSizedWrapGrid.ColumnSpan" Value="2"/>
                <Setter Property="VariableSizedWrapGrid.RowSpan" Value="1"/>
                <Setter Property="Width" Value="200"/>
                <Setter Property="Height" Value="100"/>
                <Setter Property="Background" Value="Blue"/>
            </Style>
            <Style x:Key="style1x2" TargetType="GridViewItem">
                <Setter Property="VariableSizedWrapGrid.ColumnSpan" Value="1"/>
                <Setter Property="VariableSizedWrapGrid.RowSpan" Value="2"/>
                <Setter Property="Width" Value="100"/>
                <Setter Property="Height" Value="200"/>
                <Setter Property="Background" Value="White"/>
            </Style>
            <local:MyStyleSelector x:Key="setItemContainerStyleSelector"
                style1x1="{StaticResource style1x1}"
                style1x2="{StaticResource style1x2}"
                style2x1="{StaticResource style2x1}"
                style2x2="{StaticResource style2x2}"/>
    
    
            <GroupStyle x:Key="groupStyle1">
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid VerticalAlignment="Bottom">
                            <Button Content="{Binding Title}" Style="{StaticResource TextButtonStyle}" Foreground="Purple"/>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid MaximumRowsOrColumns="4" HorizontalAlignment="Center" VerticalAlignment="Top"  Orientation="Vertical" ItemHeight="100" ItemWidth="100"/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
            <GroupStyle x:Key="groupStyle2">
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid VerticalAlignment="Bottom">
                            <Button Content="{Binding Title}" Style="{StaticResource TextButtonStyle}" Foreground="Yellow"/>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid MaximumRowsOrColumns="4" HorizontalAlignment="Center" VerticalAlignment="Top"  Orientation="Vertical" ItemHeight="100" ItemWidth="100"/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
            <GroupStyle x:Key="groupStyle3">
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid VerticalAlignment="Bottom">
                            <Button Content="{Binding Title}" Style="{StaticResource TextButtonStyle}" Foreground="Pink"/>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid MaximumRowsOrColumns="4" HorizontalAlignment="Center" VerticalAlignment="Top"  Orientation="Vertical" ItemHeight="100" ItemWidth="100"/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
            <GroupStyle x:Key="groupStyle4">
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid VerticalAlignment="Bottom">
                            <Button Content="{Binding Title}" Style="{StaticResource TextButtonStyle}" Foreground="Orange"/>
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid MaximumRowsOrColumns="4" HorizontalAlignment="Center" VerticalAlignment="Top"  Orientation="Vertical" ItemHeight="100" ItemWidth="100"/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
            <local:MyGroupStyleSelector x:Key="setGroupStyleSelector" 
                                        groupStyle1="{StaticResource groupStyle1}"
                                        groupStyle2="{StaticResource groupStyle2}"
                                        groupStyle3="{StaticResource groupStyle3}"
                                        groupStyle4="{StaticResource groupStyle4}"/>
    
            <CollectionViewSource x:Name="cvs" Source="{Binding}" IsSourceGrouped="True" ItemsPath="Items"/>
            
        </Page.Resources>
        
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <GridView ItemsSource="{Binding Source={StaticResource cvs}}" 
                      ItemContainerStyleSelector="{StaticResource setItemContainerStyleSelector}"
                      GroupStyleSelector="{StaticResource setGroupStyleSelector}" >
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
                
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <Grid/>
                    </DataTemplate>
                </GridView.ItemTemplate>
    
                <GridView.GroupStyle>
                    <GroupStyle >
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <Grid VerticalAlignment="Bottom">
                                    <Button Content="{Binding Title}" Style="{StaticResource TextButtonStyle}"/>
                                </Grid>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <VariableSizedWrapGrid MaximumRowsOrColumns="4" HorizontalAlignment="Center" VerticalAlignment="Top"  Orientation="Vertical" ItemHeight="100" ItemWidth="100"/>
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </GridView.GroupStyle>
    
            </GridView>
        </Grid>
    
    </Page>
    
    
    
    
    

    目前的情况是只有一个默认的GroupStyle;应该是由于我写了默认的GroupStyle

                <GridView.GroupStyle>
                    <GroupStyle >
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <Grid VerticalAlignment="Bottom">
                                    <Button Content="{Binding Title}" Style="{StaticResource TextButtonStyle}"/>
                                </Grid>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.Panel>
                            <ItemsPanelTemplate>
                                <VariableSizedWrapGrid MaximumRowsOrColumns="4" HorizontalAlignment="Center" VerticalAlignment="Top"  Orientation="Vertical" ItemHeight="100" ItemWidth="100"/>
                            </ItemsPanelTemplate>
                        </GroupStyle.Panel>
                    </GroupStyle>
                </GridView.GroupStyle>

    入上图;

    可是如果我把这个默认的去了,就是这个情况:

    如何才能使用好GroupStyleSelector;

    [参考过这段对于GroupStyleSelector的讨论,他们也有类似的问题待解决:

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/5f12273f-e000-4c96-a4bc-6ccc18a104a0]

    请帮助我改变下相关的代码使得获得我想要的效果:[不同的标题字体会出现不同颜色]因为我在不同的GroupStyle里对标题的字体颜色各自做了设置

    感谢



    2013年1月23日 1:45

答案