如何在VariableSizedWrapGrid周围放置边框?

时间:2013-02-03 12:44:37

标签: xaml windows-8 windows-runtime winrt-xaml

这个让我发疯,我相信必须有一个直截了当的答案(我无法发现)。

我有一个分组的gridview控件,它使用VariableSizedWrapGrid作为分组面板。我的客户批准的设计包括每组的顶部和底部边框。我以为我可以做两件事之一:

  1. 指定VariableSizedWrapGrid上的边框;或
  2. 在GroupStyle.HeaderTemplate中创建一行并将其应用于页脚。
  3. 所以看起来我不能做任何一件事,因为VariableSizedWrapGrid继承自Panel,它不支持border属性(只将边框添加为子元素),而GridView类不包含分组的页脚属性。有没有办法将边框应用于VariableSizedWrapGrid? Xaml对我来说很新,因为我通常专注于服务器端代码而不是演示。

2 个答案:

答案 0 :(得分:1)

如果我理解正确,那么你想要达到的目标就是这样:

Result of the code below

这是代码,它也应该与variablesizegrid一起使用。如果我误解了,请添加更多详细信息和您已有的代码,以便我们了解我们如何才能最好地为您提供帮助。

<common:LayoutAwarePage
x:Name="pageRoot"
x:Class="App14.ItemsPage"
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App14"
xmlns:data="using:App14.Data"
xmlns:common="using:App14.Common"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Page.Resources>
    <CollectionViewSource x:Name="groups" IsSourceGrouped="true" />
</Page.Resources>
<Grid Style="{StaticResource LayoutRootStyle}">
    <Grid.Resources>
        <DataTemplate x:Key="groupTemplate">
            <Grid>
                <Border BorderBrush="White" BorderThickness="0,10" Padding="20">
                    <StackPanel >
                        <Border Background="DarkGreen" Padding="10" Margin="10">
                            <TextBlock Text="{Binding Name}"/>
                        </Border>
                        <Border Background="Yellow" Padding="10" Margin="10">
                            <Image Width="100" Height="100" Stretch="Uniform" Source="{Binding Image}"/>
                        </Border>
                    </StackPanel>
                </Border>
            </Grid>
        </DataTemplate>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="140"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <GridView
        x:Name="itemGridView"
        AutomationProperties.AutomationId="ItemsGridView"
        AutomationProperties.Name="Items"
        TabIndex="1"
        Grid.RowSpan="2"
        Padding="116,136,116,46"
        ItemsSource="{Binding Source={StaticResource groups}}"
        ItemTemplate="{StaticResource groupTemplate}">
        <GridView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid Margin="10">
                            <TextBlock Text='{Binding Key}' Foreground="White" FontSize="25" Margin="5" />
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </GridView.GroupStyle>
    </GridView>
</Grid>

和代码:

namespace App14

{     public sealed partial class ItemsPage:App14.Common.LayoutAwarePage     {         public ItemsPage()         {             this.InitializeComponent();

        groups.Source = GetAllGrouped(LoadCats());
    }
    public IEnumerable<IGrouping<string, FakeCat>> GetAllGrouped(IEnumerable<FakeCat> cats)
    {
        return cats.OrderBy(x => x.Name).GroupBy(x => x.Name);
    }

    IEnumerable<FakeCat> LoadCats()
    {
        return new List<FakeCat>
                   {
                       new FakeCat {Name = "Naomi", Image = "../Assets/cat1.jpg"},
                       new FakeCat {Name = "Naomi", Image = "../Assets/cat2.jpg"},
                       new FakeCat {Name = "Peter", Image = "../Assets/cat3.jpg"},
                       new FakeCat {Name = "Spencer", Image = "../Assets/cat4.jpg"},
                   };
    }
}
public class FakeCat
{
    public string Name { get; set; }
    public string Image { get; set; }
    public int ItemSize { get; set; }
}

}

答案 1 :(得分:0)

问题解决了!我想我无法弄清楚什么控制组的模板而不是实际的项目。我想赞成解决这个问题,但答案来自LinkedIn组的一名成员。应用于GridView的GroupStyle的ContainerStyle时,以下样式有效:

<Style x:Key="GroupItemStyle1" TargetType="GroupItem">
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="GroupItem">
        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
        <Grid>
            <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
        <ContentControl x:Name="HeaderContent" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" ContentTemplateSelector="{TemplateBinding ContentTemplateSelector}" Content="{TemplateBinding Content}" IsTabStop="False" Margin="{TemplateBinding Padding}" TabIndex="0"/>
        <Rectangle VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Height="1" Fill="White" Margin="5,0,15,0" />
        <ItemsControl x:Name="ItemsControl" IsTabStop="False" ItemsSource="{Binding GroupItems}" Grid.Row="1" TabIndex="1" TabNavigation="Once">
            <ItemsControl.ItemContainerTransitions>
                <TransitionCollection>
                    <AddDeleteThemeTransition/>
                    <ContentThemeTransition/>
                    <ReorderThemeTransition/>
                    <EntranceThemeTransition IsStaggeringEnabled="False"/>
                </TransitionCollection>
            </ItemsControl.ItemContainerTransitions>
        </ItemsControl>
        <Rectangle Grid.Row="1" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Height="1" Fill="White" Margin="5,0,15,0" />
        </Grid>
        </Border>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
</Style>

然后在GridView的XAML中:

<GridView.GroupStyle>
    <GroupStyle  HidesIfEmpty="True" ContainerStyle="{StaticResource GroupItemStyle1}">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <!-- Header Template here  -->
            </DataTemplate>
        </GroupStyle.HeaderTemplate>

        <GroupStyle.Panel>

            <ItemsPanelTemplate>
                <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,0,0"/>
            </ItemsPanelTemplate>

        </GroupStyle.Panel>
    </GroupStyle>
</GridView.GroupStyle>