大家好我跟着这个例子MSDN Group items in listbox 但由于某种原因,只有第一组数据显示在屏幕上,因此不会滚动。
当我运行应用程序时,屏幕仅显示“项目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; }
}
}
答案 0 :(得分:0)
始终远离使用fix-width值。尽量不要始终为元素赋予Width和Height属性。在ColumnDefinitions / RowDefinitions中使用百分比调整大小或启动(*)。
我没有看到代码背后的任何错误(CS)。 XAML也可以,但您需要更改以下行:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
并且不要使用像Height =&#34; 9000&#34; ,绝不:)