我正在尝试在 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))));