在Windows应用商店中更新GridView

时间:2014-01-08 23:19:03

标签: c# xaml data-binding windows-store-apps

我正在使用C#/ XAML为Windows商店(MVVM模式)开发应用程序。 当前视图显示类别及其课程列表,如下所示。

<CollectionViewSource x:Name="groupedItemsViewSource"
                      Source="{Binding Path=ListCourses}"
                      IsSourceGrouped="true"
                      ItemsPath="Courses" />

GridView中的课程列表:

<GridView
   x:Name="itemGridView"
   AutomationProperties.AutomationId="ItemGridView"
   AutomationProperties.Name="Grouped Items"
   Grid.RowSpan="2"
   Padding="116,35,40,46"
   SelectionMode="Single"
   ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
   ItemTemplate="{StaticResource ListCourseItemTemplate}"
   SelectedItem="{Binding SelectedItem, Mode=TwoWay}"         
   IsSwipeEnabled="false"
   IsItemClickEnabled="True">      
     <GridView.ItemsPanel>
          <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal"/>
           </ItemsPanelTemplate>
     </GridView.ItemsPanel>
     <GridView.GroupStyle>
         <GroupStyle>
             <GroupStyle.HeaderTemplate>
                 <DataTemplate>
                     <Grid Margin="1,0,0,6">
                         <Button AutomationProperties.Name="Group Title"
                                 Foreground="{StaticResource WShopperAccentTextBrush}"
                                 Style="{StaticResource TextPrimaryButtonStyle}">
                                 <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Name}" Margin="3,-7,10,10" Style="{StaticResource GroupHeaderTextStyle}" />
                                    <TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>
                                 </StackPanel>
                          </Button>
                      </Grid>
                 </DataTemplate>
             </GroupStyle.HeaderTemplate>
             <GroupStyle.Panel>
                  <ItemsPanelTemplate>
                      <VariableSizedWrapGrid Orientation="Vertical" Margin="0,0,80,0"/>
                  </ItemsPanelTemplate>
              </GroupStyle.Panel>
         </GroupStyle>
     </GridView.GroupStyle>
</GridView>

班级类别:

public Category()
{

}
public int Id { get; set; }

public string Name { get; set; }


public IEnumerable<Course> Courses { get; set; }

在我的视图模型中,-ListCourses属性如下所示:

public ObservableCollection<Category> ListCourses
{
    get { return _listCourses; }
    private set { SetProperty(ref _listCourses, value); }
}

我的问题是:当我从列表中删除一个课程(从gridView中删除项目)时,gridView不会更新并显示ListCourses属性中的当前数据吗?

删除功能:

     var category = ListCourses.FirstOrDefault(cat => cat.Id == SelectedItem.CategoryId);
        category.Courses.Remove(SelectedItem);
        OnPropertyChanged("ListCourses");

任何想法?

1 个答案:

答案 0 :(得分:2)

category.Courses也必须是ObservableCollection