C# 为什么我能';是否将自定义类作为组绑定到GridView?
我试图将一些项目绑定到我的gridview,但我也希望将它们分组。首先,我的xaml看起来像这样C# 为什么我能';是否将自定义类作为组绑定到GridView?,c#,gridview,data-binding,windows-store-apps,winrt-xaml,C#,Gridview,Data Binding,Windows Store Apps,Winrt Xaml,我试图将一些项目绑定到我的gridview,但我也希望将它们分组。首先,我的xaml看起来像这样 <Page.Resources> <!-- TODO: Delete this line if the key AppName is declared in App.xaml --> <x:String x:Key="AppName">Header</x:String> <CollectionVi
<Page.Resources>
<!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
<x:String x:Key="AppName">Header</x:String>
<CollectionViewSource x:Name="itemsViewSource" IsSourceGrouped="True" Source="{Binding Items}"/>
<DataTemplate x:Key="DataTemplate1">
<Grid HorizontalAlignment="Left" Width="168" Height="157">
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
<Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
</Border>
<StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}" Height="48">
<TextBlock Text="{Binding Title}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="59" Margin="16,0,7,0" FontSize="16" FontFamily="Andy"/>
</StackPanel>
</Grid>
</DataTemplate>
</Page.Resources>
<!--
This grid acts as a root panel for the page that defines two rows:
* Row 0 contains the back button and page title
* Row 1 contains the rest of the page layout
-->
<Grid x:Name="grd" Style="{StaticResource LayoutRootStyle}">
<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 itemsViewSource}}"
ItemTemplate="{StaticResource DataTemplate1}"
SelectionMode="None"
IsSwipeEnabled="false"
IsItemClickEnabled="True"
ItemClick="ItemView_ItemClick">
<GridView.Background>
<ImageBrush ImageSource="Assets/bg4.jpg"/>
</GridView.Background>
<GridView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Grid Margin="1,0,0,6">
<Button
AutomationProperties.Name="Group Title"
Style="{StaticResource TextPrimaryButtonStyle}" Content="" />
</Grid>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</GridView.GroupStyle>
</GridView>
<!-- Back button and page title -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}"/>
<TextBlock x:Name="pageTitle" Grid.Column="1" Style="{StaticResource PageHeaderTextStyle}" FontFamily="Buxton Sketch" FontSize="60" Foreground="#DEFFFFFF" Margin="0,0,30,44">
<Run Foreground="#DE316191" Text="Merak"/>
<Run FontSize="48" Text=" "/>
<Run Foreground="#DEFDFDFD" Text="Edilen"/>
<Run FontSize="48" Text=" "/>
<Run Foreground="#DE3F6B97" Text="Şeyler"/>
</TextBlock>
</Grid>
<VisualStateManager.VisualStateGroups>
<!-- Visual states reflect the application's view state -->
<VisualStateGroup x:Name="ApplicationViewStates">
<VisualState x:Name="FullScreenLandscape"/>
<VisualState x:Name="Filled"/>
<!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
<VisualState x:Name="FullScreenPortrait">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<!-- The back button and title have different styles when snapped -->
<VisualState x:Name="Snapped">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
<DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
标题
有了这个,我可以用这个代码创建组
public class Item
{
public string Title { get; set; }
public string Description{ get; set; }
public string Image { get; set; }
}
List<List<Item>> total = new List<List<Item>>();
List<Item> lst = new List<Item>();
public void AddGroups()
{
Item first= new Item();
first.Title = "asdf";
first.Description = "asdaf";
first.Image = "aswdfa";
lst.Add(first);
total.Add(lst);
}
公共类项目
{
公共字符串标题{get;set;}
公共字符串说明{get;set;}
公共字符串图像{get;set;}
}
列表总数=新列表();
List lst=新列表();
公共void AddGroups()
{
项目优先=新项目();
首先,Title=“asdf”;
首先,Description=“asdaf”;
首先,Image=“aswdfa”;
第一次添加(第一次);
总计。添加(lst);
}
有了这段代码,我可以添加没有标题文本的组,但是我需要标题文本,没有它我什么都做不了。所以我尝试创建另一个自定义类,并将这个新列表绑定到gridview。我试着创建一个这样的类,然后绑定这个类的列表,但它不起作用,显示一个没有任何项目的空白屏幕
public class grp
{
private List<Item> bilg;
public grp(List<Item> bilg)
{
this.bilg = bilg;
}
}
公共类grp
{
私人名单诈骗案;
公共grp(清单bilg)
{
这个。舱底=舱底;
}
}
第二节课可能完全错了,尽管我不知道。这是一个Windows应用商店应用程序,我使用的是xaml和c。谢谢你的帮助
tl;dr.我正在尝试创建一个gridview,其中包含组名在其上的组。不久前,我不得不创建一个类似的控件。我使用HeaderTemplates在网格中显示标题值。但我不确定这是否对你有帮助 代码示例 像这样的东西可能有用
<HeaderTemplate>
<asp:Label ID="label" runat="server" Text='<%= Eval("text") %>' />
</HeaderTemplate>
请查看此示例:
我希望这有帮助 检查第一个代码,我有一个HeaderTemplate,但我不知道如何使用它。你能给我一些样品吗?哇。我现在看到了。添加标签或任何需要的控件,以便可以在标题模板中为其设置文本。您可能还需要向控件添加eval或bind。在“代码隐藏”中,使用“查找”控件在网格上查找标签。我的问题是使用Windows应用商店应用程序而不是asp。没有任何类型的标签控件可供使用?我相信基本面是相似的。您想在HeaderTemplate中放置一个控件来保存文本。谢谢,我将对此进行研究,看起来比VisualStudio更简单。
Label lbl = (Label)gridview.FindControl('LabelId');
lbl.text = "your text";