在windows8.1中分组列表视图不显示所有数据
本文关键字:显示 数据 视图 列表 windows8 | 更新日期: 2023-09-27 17:51:18
大家好,我遵循这个示例MSDN组项列表框但由于某种原因,只有第一组数据显示在屏幕上,不能滚动。
当我运行应用程序时,屏幕只显示"项目1"作为标题,然后在它下面显示5个活动,但不显示其余的数据。我无法弄清楚是否是高度或滚动不工作,并显示其余组(项目2,项目3等)。
如果有人能看到任何明显的错误,我正在犯请让我知道?
提前谢谢你。
MainPage.xaml
<Page
x:Class="TestWin._1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TestWin._1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<CollectionViewSource x:Name="cvsActivities" IsSourceGrouped="True">
</CollectionViewSource>
<CollectionViewSource x:Name="cvsProjects" IsSourceGrouped="True" ItemsPath="Activities"/>
<local:ListGroupStyleSelector x:Key="listGroupStyleSelector"/>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ListView ItemsSource="{Binding Source={StaticResource cvsActivities}}"
ItemTemplate="{StaticResource listViewItemTemplate}"
GroupStyleSelector="{StaticResource listGroupStyleSelector}"
Margin="120" Width="320" Height="9000"
/>
<GridView ItemsSource="{Binding Source={StaticResource cvsProjects}}"
Margin="0,10,0,0" Grid.Column="1" ScrollViewer.VerticalScrollMode="Auto">
<GridView.ItemTemplate>
<DataTemplate>
<StackPanel Margin="20">
<TextBlock Text="{Binding Name}" FontWeight="Bold"
Style="{StaticResource BaseTextBlockStyle}"/>
<TextBlock Text="{Binding DueDate}" TextWrapping="NoWrap"
Style="{StaticResource BodyTextBlockStyle}" />
<CheckBox Content="Complete" IsChecked="{Binding Complete}"
IsEnabled="False"/>
</StackPanel>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid MaximumRowsOrColumns="3"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
MainPage.cs
using System;
namespace TestWin._1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
DateTime startDate;
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
DateTime.TryParse("1/1/2014", out startDate);
PopulateProjects();
PopulateActivities();
}
private void PopulateActivities()
{
List<Activity> Activities = new List<Activity>();
Activities.Add(new Activity()
{
Name = "Activity 1",
Complete = true,
DueDate = startDate.AddDays(4),
Project = "Project 1"
});
Activities.Add(new Activity()
{
Name = "Activity 2",
Complete = true,
DueDate = startDate.AddDays(5),
Project = "Project 1"
});
Activities.Add(new Activity()
{
Name = "Activity 3",
Complete = false,
DueDate = startDate.AddDays(7),
Project = "Project 1"
});
Activities.Add(new Activity()
{
Name = "Activity 4",
Complete = false,
DueDate = startDate.AddDays(9),
Project = "Project 1"
});
Activities.Add(new Activity()
{
Name = "Activity 5",
Complete = false,
DueDate = startDate.AddDays(14),
Project = "Project 1"
});
Activities.Add(new Activity()
{
Name = "Activity A",
Complete = true,
DueDate = startDate.AddDays(2),
Project = "Project 2"
});
Activities.Add(new Activity()
{
Name = "Activity B",
Complete = false,
DueDate = startDate.AddDays(4),
Project = "Project 2"
});
Activities.Add(new Activity()
{
Name = "Activity C",
Complete = true,
DueDate = startDate.AddDays(5),
Project = "Project 2"
});
Activities.Add(new Activity()
{
Name = "Activity D",
Complete = false,
DueDate = startDate.AddDays(9),
Project = "Project 2"
});
Activities.Add(new Activity()
{
Name = "Activity E",
Complete = false,
DueDate = startDate.AddDays(18),
Project = "Project 2"
});
var result = from act in Activities group act by act.Project into grp orderby grp.Key select grp;
cvsActivities.Source = result;
}
private void PopulateProjects()
{
List<Project> Projects = new List<Project>();
Project newProject = new Project();
newProject.Name = "Project 1";
newProject.Activities.Add(new Activity() { Name = "Activity 1", Complete = true, DueDate = startDate.AddDays(4) });
newProject.Activities.Add(new Activity() { Name = "Activity 2", Complete = true, DueDate = startDate.AddDays(5) });
newProject.Activities.Add(new Activity() { Name = "Activity 3", Complete = false, DueDate = startDate.AddDays(7) });
newProject.Activities.Add(new Activity() { Name = "Activity 4", Complete = false, DueDate = startDate.AddDays(9) });
newProject.Activities.Add(new Activity() { Name = "Activity 5", Complete = false, DueDate = startDate.AddDays(14) });
Projects.Add(newProject);
newProject = new Project();
newProject.Name = "Project 2";
newProject.Activities.Add(new Activity() { Name = "Activity A", Complete = true, DueDate = startDate.AddDays(2) });
newProject.Activities.Add(new Activity() { Name = "Activity B", Complete = false, DueDate = startDate.AddDays(3) });
newProject.Activities.Add(new Activity() { Name = "Activity C", Complete = true, DueDate = startDate.AddDays(5) });
newProject.Activities.Add(new Activity() { Name = "Activity D", Complete = false, DueDate = startDate.AddDays(9) });
newProject.Activities.Add(new Activity() { Name = "Activity E", Complete = false, DueDate = startDate.AddDays(18) });
Projects.Add(newProject);
newProject = new Project();
newProject.Name = "Project 3";
Projects.Add(newProject);
cvsProjects.Source = Projects;
}
}
public class ListGroupStyleSelector : GroupStyleSelector
{
protected override GroupStyle SelectGroupStyleCore(object group, uint level)
{
return (GroupStyle)App.Current.Resources["listViewGroupStyle"];
}
}
public class Project
{
public Project()
{
Activities = new ObservableCollection<Activity>();
}
public string Name { get; set; }
public ObservableCollection<Activity> Activities { get; private set; }
}
public class Activity
{
public string Name { get; set; }
public DateTime DueDate { get; set; }
public bool Complete { get; set; }
public string Project { get; set; }
}
}
始终避免使用固定宽度值。尽量不要总是给元素赋予宽度和高度属性。在ColumnDefinitions/RowDefinitions中使用按百分比调整大小或开始(*)。
我没有看到任何错误的代码背后(CS)。XAML也可以,但是您需要更改以下行:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
不要使用Height="9000", NEVER:)