0

Windows 8 アプリケーションでグループ化されたグリッドビューを作成しようとしています。そのページの ViewModel クラスで ObservableCollection を作成した book という名前のクラスがあります。しかし、XAML の codebind ファイルでデータをグループ化してバインドしようとしているときにデータをグループ化する必要があり、バインドしても INotifyProperty の影響はありません。VWでこれを達成するにはどうすればよいか教えてください。

以下のコード:

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

入力に従って変更: Tuple の 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 コレクション /// /// プロパティの名前。/// 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());

IOrderedEnumerable監視可能なコレクションをラップし、ビューにバインドするものを公開するlinqステートメントを作成します。そして、このインターフェイスは明らかに、コレクションやプロパティからの変更イベントを転送しません。

これを解決するには、既にソートおよびグループ化されて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))));

2 番目のプロパティをクレートする場合は、本が追加、削除、またはカテゴリが変更されたときに同期する必要があることに注意してください。

于 2013-01-23T07:32:29.330 に答える