2

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

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

  1. 在 VariableSizedWrapGrid 上指定边框;或者
  2. 在 GroupStyle.HeaderTemplate 中创建一行并将其应用于页脚。

所以看起来我不能做任何这些事情,因为 VariableSizedWrapGrid 继承自 Panel 不支持边框属性(仅添加边框作为子元素)并且 GridView 类不包含分组页脚属性。有没有办法为 VariableSizedWrapGrid 应用边框?Xaml 对我来说很新,因为我通常专注于服务器端代码而不是演示文稿。

4

2 回答 2

1

如果我对您的理解正确,那么您要实现的目标是这样的:

下面代码的结果

这是它的代码,它也应该与 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

{ 公共密封部分类 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; }
}

}

于 2013-02-03T18:39:44.423 回答
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>
于 2013-02-13T12:14:25.343 回答