Windows应用商店应用:网格视图组绑定背景颜色?

时间:2015-08-11 15:46:11

标签: c# .net xaml windows-store-apps

我有一个带有CollectionViewSource的网格视图,因为它是项目源。

我想绑定每个组容器面板的background属性,以便每个组都有自己的背景颜色。

如何实现这一目标?

我正在尝试在gridview的<GroupStyle.ContainerStyle>中使用绑定,但无法使其工作。

2 个答案:

答案 0 :(得分:1)

由于列表已经分组,然后在每个GridViewItem上应用背景将会起作用,具体取决于您是要将每个项目中的backgound定义为属性还是使用转换器做到这一点:

public class Data
{
    public String Prop1 { get; set; }
    public String Prop2 { get; set; }
    public SolidColorBrush GroupeBrush { get; set; } //the groupe background color
}

和xaml,

<Page.Resources>
    <CollectionViewSource x:Name="DataCollection" IsSourceGrouped="true" />
</Page.Resources>

<Grid>
    <GridView SelectionMode="None"   ItemsSource="{Binding Source={StaticResource DataCollection}}" >
        <GridView.ItemContainerStyle>
            <Style TargetType="GridViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
            </Style>
        </GridView.ItemContainerStyle>
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
        <GridView.ItemTemplate>
            <DataTemplate>
                <Grid Background="{Binding GroupeBrush}">
                    <TextBlock Text="{Binding Prop2}" />
                </Grid>                    
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
</Grid>

或者您也可以围绕GridView GroupStyle玩游戏,尽管您需要找到一种方法来绑定background Style中的Setter

<GridView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Border Background="Black" HorizontalAlignment="Stretch">
                            <TextBlock Text='{Binding Key}' Foreground="White" Margin="5" Style="{StaticResource SubheaderTextBlockStyle}" />
                        </Border>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>

                <GroupStyle.ContainerStyle>
                    <Style TargetType="GroupItem">
                        <Setter Property="MinWidth" Value="600"/>
                        <Setter Property="BorderBrush" Value="DarkGray"/>
                        <Setter Property="BorderThickness" Value="2"/>
                        <Setter Property="Margin" Value="3,0"/>
                        <Setter Property="Background" Value="BurlyWood"/>
                    </Style>
                </GroupStyle.ContainerStyle>                   
            </GroupStyle>
        </GridView.GroupStyle>

这里有完整的代码,以防任何人想要试验更多

public sealed partial class MainPage : Page
{
    private ObservableCollection<Data> _datas = new ObservableCollection<Data>()
    {
        new Data()
        {
            Prop1 = "val1",
            Prop2 = "val2",
            GroupeBrush=new SolidColorBrush(Colors.Blue)
        }, new Data()
        {
            Prop1 = "val1",
            Prop2 = "val2",
            GroupeBrush=new SolidColorBrush(Colors.Blue)
        }, new Data()
        {
            Prop1 = "val1",
            Prop2 = "val3",
            GroupeBrush=new SolidColorBrush(Colors.Blue)
        }, new Data()
        {
            Prop1 = "val2",
            Prop2 = "val4",
            GroupeBrush=new SolidColorBrush(Colors.Green)
        }, new Data()
        {   
            Prop1 = "val3",
            Prop2 = "val5",
            GroupeBrush=new SolidColorBrush(Colors.Red)
        },
    };

    public ObservableCollection<Data> Datas
    {
        get
        {
            return _datas;
        }

        set
        {
            if (_datas == value)
            {
                return;
            }

            _datas = value;                
        }
    }
    public MainPage()
    {
        this.DataContext = this;
        InitializeComponent();
        DataCollection.Source = GetAllGrouped();
    }
    public IEnumerable<IGrouping<string, Data>> GetAllGrouped()
    {
        return Datas.GroupBy(x => x.Prop1);
    }      
}

public class Data
{
    public String Prop1 { get; set; }
    public String Prop2 { get; set; }
    public SolidColorBrush GroupeBrush { get; set; } //the groupe background color
}

答案 1 :(得分:1)

好的,这就是我所拥有的。我必须修改组容器样式模板

<GroupStyle.ContainerStyle>
                            <Style TargetType="GroupItem">
                                <Setter Property="IsTabStop" Value="False" />
                                <Setter Property="Margin" Value="10,0,0,0" />
                                <Setter Property="Padding" Value="20"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="GroupItem">
                                            <Border Background="{Binding Group, Converter={StaticResource ThemeColorConverter}}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="Auto" />
                                                        <RowDefinition Height="*" />
                                                    </Grid.RowDefinitions>
                                                    <ContentControl x:Name="HeaderContent"
                                        Content="{TemplateBinding Content}"
                                        ContentTransitions="{TemplateBinding ContentTransitions}"
                                        ContentTemplate="{TemplateBinding ContentTemplate}"
                                        ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}"
                                        Margin="{TemplateBinding Padding}"
                                        TabIndex="0"
                                        IsTabStop="False" />
                                                    <ItemsControl x:Name="ItemsControl"
                                      Grid.Row="1"
                                      ItemsSource="{Binding GroupItems}"
                                      IsTabStop="False"
                                      TabNavigation="Once"
                                      TabIndex="1" >
                                                        <ItemsControl.ItemContainerTransitions>
                                                            <TransitionCollection>
                                                                <AddDeleteThemeTransition />
                                                                <ContentThemeTransition />
                                                                <ReorderThemeTransition />
                                                                <EntranceThemeTransition IsStaggeringEnabled="False" />
                                                            </TransitionCollection>
                                                        </ItemsControl.ItemContainerTransitions>
                                                    </ItemsControl>
                                                </Grid>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>

这条线就行了:

<Border Background="{Binding Group, Converter={StaticResource ColorConverter}}"/>

绑定到群组可让您访问群组的数据来源。