我有一个[表格] Book
,其中列为[列] Title | Author | Editor | Genre
我使用Pivot模板,标题为Title,Author,Editor,Genre。 示例:我有3本书:
1 / Title =“ABC”|作者=“约翰”|编辑=“杰瑞”|类型=“小说”
2 / Title =“EFH”|作者=“约翰”|编辑=“哈利”|类型=“神秘”
3 / Title =“HJK”|作者=“汤姆”|编辑=“杰瑞”|类型=“神秘”
我想要的是每个标题只显示:
标题标题中的:ABC | EFH | HJK
只显示:John | Tom
只显示:Jerry | Harry
只显示:Novel | Mystery
代码:
<!--Book-->
<controls:PivotItem Header="Book">
<Grid x:Name="ContentPanel1" Margin="12,0,12,0">
<ListBox x:Name="TitleList" SelectionChanged="TitleList_SelectionChanged" ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="466" Margin="0, 0, 0, 12">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="360"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0"></Grid>
<StackPanel Grid.Column="1">
<TextBlock FontSize="40" Text="{Binding BookTitle}" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>
</StackPanel>
<Grid Grid.Column="2">
<Button x:Name="Deletebutton" Height="60" Width="60" Click="deleteButton_Click" BorderBrush="{StaticResource TransparentBrush}">
<Image Source="/Assets/delete.dark.png" Visibility="{StaticResource PhoneDarkThemeVisibility}" Margin="-25" HorizontalAlignment="Left"/>
</Button>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
<!--Author-->
<controls:PivotItem Header="Author">
<Grid x:Name="ContentPanel2" Margin="12,0,12,0">
<ListBox x:Name="AuthorList" ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock FontSize="40" Text="{Binding Author}" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
<!--Editor-->
<controls:PivotItem Header="Editor">
<Grid x:Name="ContentPanel3" Margin="12,0,12,0">
<ListBox x:Name="EditorList" ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock FontSize="40" Text="{Binding Editor}" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
<!--Editor-->
<controls:PivotItem Header="Editor">
<Grid x:Name="ContentPanel4" Margin="12,0,12,0">
<ListBox x:Name="BookImporterList" ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock FontSize="40" Text="{Binding Editor}" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
<!--Genre-->
<controls:PivotItem Header="Genre">
<Grid x:Name="ContentPanel5" Margin="12,0,12,0">
<ListBox x:Name="GenreList" ItemsSource="{Binding Data}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock FontSize="40" Text="{Binding Genre}" FontWeight="Normal" FontStyle="Normal" Style="{StaticResource PhoneTextTitle3Style}" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</controls:PivotItem>
</controls:Pivot>
在我的MainViewModel中:
private ObservableCollection<Book> _load = new ObservableCollection<Book>();
public ObservableCollection<Book> Load
{
get { return _load; }
set
{
_load = value;
NotifyPropertyChanged("Load");
}
}
public void LoadData()
{
var orderedGenre = (from Book b in BookDB.Books select b);
}
答案 0 :(得分:1)
这是获得你想要的东西的一种可能方式。
在ViewModel中提供了一个存储所有Books的属性,在这种情况下它是Load
属性。然后为要显示的每个Book的属性声明新属性。它们中的每一个都从相应的Book属性返回不同值的集合。在Load
属性的setter中,我为从Load
属性返回值的所有属性引发了PropertyChanged事件。
我在这里提供了两种方法,通过使用Distinct方法或使用GroupBy方法从Book的ObservableCollection中获取不同的值。但我更喜欢第一种方式,因为它在这种情况下更短/更简单。
private ObservableCollection<Book> _load = new ObservableCollection<Book>();
public ObservableCollection<Book> Load
{
get { return _load; }
set
{
_load = value;
NotifyPropertyChanged("Load");
NotifyPropertyChanged("Titles");
NotifyPropertyChanged("Authors");
}
}
public ObservableCollection<String> Titles
{
get { return new ObservableCollection<string>(Load.Select(o => o.Title).Distinct()); }
//get { return new ObservableCollection<string>(Load.GroupBy(o => o.Title).Select(o => o.Key)); }
}
public ObservableCollection<String> Authors
{
get { return new ObservableCollection<string>(Load.Select(o => o.Author).Distinct()); }
//get { return new ObservableCollection<string>(Load.GroupBy(o => o.Title).Select(o => o.Key)); }
}
public void LoadData()
{
var orderedGenre = (from Book b in BookDB.Books select b);
Load = new ObservableCollection<Book>(orderedGenre);
}
然后在XAML中,将每个ListBox的ItemsSource绑定到已在ViewModel中创建的相应属性:
<controls:PivotItem Header="Book">
<Grid x:Name="ContentPanel1" Margin="12,0,12,0">
<ListBox x:Name="TitleList" SelectionChanged="TitleList_SelectionChanged" ItemsSource="{Binding Titles}">
....
....
<controls:PivotItem Header="Author">
<Grid x:Name="ContentPanel2" Margin="12,0,12,0">
<ListBox x:Name="AuthorList" ItemsSource="{Binding Authors}">
....
....