GridView未加载数据

时间:2013-12-03 04:50:31

标签: c# xaml visual-studio-2012 gridview

我试图在点击/点击页面上另一个GridView的TextBlock后将数据加载到GridView中。第一个包含TextBlocks列表的GridView正确加载。

这是我的两个GridView的XAML代码,我的Bindings似乎是正确的:

   <GridView x:Name="CourseNoGridView" Margin="50,50,0,0" Grid.Row="1" VerticalAlignment="Top" Height="568" ItemsSource="{Binding Distinct_CourseNo}" SelectionMode="Single" Padding="0,0,0,10" HorizontalAlignment="Left" Width="525" SelectionChanged="CourseNoGridView_SelectionChanged">
        <GridView.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="White">
                    <TextBlock x:Name="CourseNoTextBlock" Text="{Binding CourseNo}" TextWrapping="NoWrap" FontSize="24" Width="200" Height="Auto" Padding="10" Tapped="CourseNoTextBlock_Tapped"/>
                </Border>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
    <GridView x:Name="SectionsGridView" Margin="580,50,0,0" Grid.Row="1" VerticalAlignment="Top" Height="568" ItemsSource="{Binding Clicked_CourseNo_Sections}" SelectionMode="Single" Padding="0,0,0,10" HorizontalAlignment="Left" Width="776" SelectionChanged="CourseNoGridView_SelectionChanged">
        <GridView.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="White">
                    <TextBlock x:Name="SectionTextBlock" Text="{Binding Get_Section}" TextWrapping="NoWrap" FontSize="24" Width="200" Height="Auto" Padding="10"/>
                </Border>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

以下是处理第一个GridView中项目点击/点击的代码:

private void CourseNoGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    clickedSection = (Sections)e.AddedItems[0];
}

private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
    this.Clicked_CourseNo_Sections = (from s in Roster_Sections
                                      where s.CourseNo.Equals(clickedSection.CourseNo)
                                      select s).ToList();
}

2 个答案:

答案 0 :(得分:1)

您要做的是使用ObservableCollection并将您的网格视图绑定到此。然后在“Tapped”事件处理程序中,清除此集合中的现有项目并添加新项目。

这样的事情:

private readonly ObservableCollection<Sections> currentSections = new ObservableCollection<Sections>();

//This is what we bind to
public ObservableCollection<Sections> CurrentSections { get { return currentSections; } }

private void CourseNoGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  clickedSection = (Sections)e.AddedItems[0];
}

private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
{
  var courseSections = (from s in Roster_Sections
                  where s.CourseNo.Equals(clickedSection.CourseNo)
                  select s);

  CurrentSections.Clear();
  CurrentSections.AddRange(courseSections);
}

这里有一些文档:

http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx

答案 1 :(得分:0)

似乎添加下面的最后一行代码解决了这个问题。

 private void CourseNoTextBlock_Tapped(object sender, TappedRoutedEventArgs e)
        {
            this.Clicked_CourseNo_Sections = (from s in Roster_Sections
                                              where s.CourseNo.Equals(clickedSection.CourseNo)
                                              select s).ToList();

            SectionsGridView.ItemsSource = Clicked_CourseNo_Sections;
        }