【问题标题】:GridView is Not Loading DataGridView 未加载数据
【发布时间】:2013-12-03 04:50:31
【问题描述】:

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

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

   <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();
}

【问题讨论】:

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


    【解决方案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

    【讨论】:

      【解决方案2】:

      似乎在下面添加最后一行代码可以解决问题。

       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;
              }
      

      【讨论】:

      • 这可能适用于这种情况,但如果您以后没有遇到问题,我会感到惊讶。 ObservableCollection 是你应该使用的
      猜你喜欢
      • 1970-01-01
      • 2013-07-25
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      相关资源
      最近更新 更多