0

我正在尝试在 Windows 8 应用程序中创建一个分组的 gridview 我有一个名为 book 的类,我在我的 ViewModel 类中为该页面创建了它的 ObservableCollection。但是当我尝试在我的 XAML 代码绑定文件上对数据进行分组和绑定时,我需要对数据进行分组,然后绑定不会发生 INotifyProperty 的影响。请建议我如何在我的大众汽车中实现这一目标?

下面的代码:

XAML 背后的代码:

 protected override void LoadState(Object navigationParameter,
                                               Dictionary<String, Object> pageState)
        {
            MainViewModel booksList = new MainViewModel();
            var teamGroups = booksList.BooksDetail.GroupBy(team => team.BookCategory)
                                           .OrderBy(team => team.Key.ToString());

            this.groupedItemsViewSource.Source = teamGroups;
        }

查看模型类:

  #region BooksDetail Collection
        /// <summary>
        /// The <see cref="BooksDetail" /> BooksDetail
        /// </summary>
        public const string BooksDetailPropertyName = "BooksDetail";

        private ObservableCollection<Book> _booksDetailProperty = new ObservableCollection<Book>();

        /// <summary>
        /// Gets the BooksDetail property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<Book> BooksDetail
        {
            get
            {
                return _booksDetailProperty;
            }

            set
            {
                if (_booksDetailProperty == value)
                {
                    return;
                }

                _booksDetailProperty = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(BooksDetailPropertyName);
            }
        }
        #endregion

  public void GetBooksDetail()
        {

                try
                {
                    // Check if the code is in Design Mode or Running Mode
                    if (ViewModelBase.IsInDesignModeStatic)
                    {
                        // Created the object of main model class
                        DesignMainModel mainModel = new DesignMainModel();

                        // Call method to get list of books detail
                        this.BooksDetail = mainModel.GetBooksDetailData();
                    }
                    else
                    {
                        // Check for internet connectivity 
                        if (Utility.IsInternetAvailable())
                        {
                            //TODO: Write the logic to to implement in Running Mode
                            // Created the object of main model class
                            DesignMainModel mainModel = new DesignMainModel();

                            // Call method to get list of books detail
                            this.BooksDetail = mainModel.GetBooksDetailData();

                            // Group by data on Book Category
                            this.GroupByBookDetailsOnCategory();
                        }
                        else
                        {
                            // Display Error Message for No internet Connection available
                            util.ShowUserMessage(App.GeneralMessageResource.GetString("InternetConnectionErrorMessage"), App.GeneralMessageResource.GetString("InternetConnectionErrorCategory"));
                        }
                    }

                }
                catch (Exception ex)
                {
                    // Display the error
                    util.ShowUserMessage(ex.Message, App.GeneralMessageResource.GetString("GeneralErrorTitle"));
                }
            }
        }

书籍类:

   #region BookCategory Property
        /// <summary>
        /// The <see cref="BookCategory" /> property's name.
        /// </summary>
        public const string BookCategoryPropertyName = "BookCategory";

        /// <summary>
        /// The _book category property
        /// </summary>
        private string _bookCategoryProperty = string.Empty;

        /// <summary>
        /// Gets the BookCategory property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string BookCategory
        {
            get
            {
                return _bookCategoryProperty;
            }

            set
            {
                if (_bookCategoryProperty == value)
                {
                    return;
                }

                _bookCategoryProperty = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(BookCategoryPropertyName);
            }
        }
        #endregion

根据输入进行更改:我创建了一个 ObservableCollection 的元组。但是现在当我将它绑定到 CollectionViewSource 时,不会填充任何列表。请参考下面的代码:让我知道我在哪里犯了错误:

XAML:

<GridView Grid.Row="1" Margin="5,0,0,0" Grid.Column="1"
         x:Name="itemGridView"
         SelectionMode="Multiple"
         ItemTemplate="{StaticResource CategoryLists}"
         ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
           ItemContainerStyle="{StaticResource CategoryListsStyle}">
    <GridView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Grid>
                                <StackPanel Orientation="Horizontal" Margin="15,0,0,0">
                                    <TextBlock Text="{Binding Key}" Margin="0,-5,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" FontFamily="Segoe UI Regular" Foreground="#00bfff" FontSize="27"/>
                                    <!--<TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-7,0,10" Style="{StaticResource GroupHeaderTextStyle}"/>-->
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <VariableSizedWrapGrid Orientation="Vertical" Margin="10,-1,35,0"/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </GridView.GroupStyle>

</Grid>

属性:#region BooksDetailGrouped Collection /// /// 属性的名称。/// public const string BooksDetailGroupedPropertyName = "BooksDetailGrouped";

        /// <summary>
        /// The _books detail grouped property
        /// </summary>
        private ObservableCollection<Tuple<string, ObservableCollection<Book>>> _booksDetailGroupedProperty = new ObservableCollection<Tuple<string, ObservableCollection<Book>>>();



        /// <summary>
        /// Gets the BooksDetailGrouped property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public ObservableCollection<Tuple<string, ObservableCollection<Book>>> BooksDetailGrouped
        {
            get
            {
                return _booksDetailGroupedProperty;
            }

            set
            {
                if (_booksDetailGroupedProperty == value)
                {
                    return;
                }

                _booksDetailGroupedProperty = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(BooksDetailGroupedPropertyName);
            }
        }
        #endregion

分组方法:

// Grouping the data
            var booksGroup = this.BooksDetail.GroupBy(books => books.BookCategory)
                                            .OrderBy(books => books.Key.ToString());

            // Convert grouped data to ObservableCollection<Tuple<string, ObservableCollection<Book>>>
            this.BooksDetailGrouped = new ObservableCollection<Tuple<string, ObservableCollection<Book>>>(
            booksGroup.Select(x => Tuple.Create(x.Key,
                                            new ObservableCollection<Book>(x))));
4

1 回答 1

0

您根本没有绑定到网格的可观察集合。

在这一行

var teamGroups = booksList.BooksDetail.GroupBy(team => team.BookCategory)
                                       .OrderBy(team => team.Key.ToString());

您创建一个 linq 语句来包装您的可观察集合并公开IOrderedEnumerable您绑定到视图的集合。而且这个接口显然不会转发来自您的集合和属性的更改事件。

要解决这个问题,您需要保留已经排序和分组的BooksDetails(或视图模型中的任何其他属性)并将其直接绑定到 xaml 中。ObservableCollection以这种方式设置源:

this.groupedItemsViewSource.Source = booksList.BooksDetail;

BooksDetail属性更改时,将不允许网格自行重新创建。


var observableCollection = new ObservableCollection<Tuple<Key, ObservableCollection<Book>>>(
          teamGroups.Select(x=>Tuple.Create(x.Key, 
                                            new ObservableCollection<Book>(x))));

如果您想创建第二个属性,请注意在添加、删除或更改类别时必须同步它们。

于 2013-01-23T07:32:29.330 回答