简体   繁体   English

如何显示Observablecollection的独特价值?

[英]How can I display distinct value from a Observablecollection?

I have a [Table] Book with columns [Column] Title | Author | Editor | Genre 我有一本[表格] Book其中有[栏] Title | Author | Editor | Genre Title | Author | Editor | Genre

I use a Pivot template with the headers Title, Author, Editor, Genre. 我使用带有标题“标题”,“作者”,“编辑器”,“类型”的数据透视模板。 Example: I have 3 books like this: 示例:我有3本这样的书:

1/ Title = "ABC" | 1 /标题=“ ABC” | Author = "John" | 作者=“约翰” | Editor = "Jerry" | 编辑=“杰里” | Genre = "Novel" 流派=“小说”

2/ Title = "EFH" | 2 /标题=“ EFH” | Author = "John" | 作者=“约翰” | Editor = "Harry" | 编辑=“哈里” | Genre = "Mystery" 类型=“神秘”

3/ Title = "HJK" | 3 /标题=“ HJK” | Author = "Tom" | 作者=“汤姆” | Editor = "Jerry" | 编辑=“杰里” | Genre = "Mystery" 类型=“神秘”

What I want is in each Header just show like this : 我想要的是在每个标题中只是这样显示

in Title header: ABC | EFH | HJK 在标题标题中: ABC | EFH | HJK ABC | EFH | HJK

in Author header just shows: John | Tom 在Author标头中仅显示: John | Tom John | Tom

in Editor header just shows: Jerry | Harry 在编辑器中头球攻门稍稍显示: Jerry | Harry Jerry | Harry

in Genre header just shows: Novel | Mystery 在流派标题中仅显示: Novel | Mystery Novel | Mystery

Code: 码:

 <!--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>

In my MainViewModel: 在我的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);
}

This is one of some possible way to get what you want. 这是获得所需内容的一些可能方式之一。

In the ViewModel provide one property to store all Books, it is Load property in this case. 在ViewModel中提供一个属性来存储所有Books,在这种情况下为Load属性。 Then declare new property for every Book's property you want to display. 然后为要显示的每个Book属性声明新属性。 Each of them returning collection of distinct values from corresponding Book property. 它们每个都从对应的Book属性返回不同值的集合。 In the setter of Load property I raise PropertyChanged event for all properties that return values from Load property. Load属性的设置器中,我为所有从Load属性返回值的属性引发PropertyChanged事件。

There are two ways I provide here to get distinct values from ObservableCollection of Book, by using Distinct method, or using GroupBy method. 我在这里提供了两种方法,可以使用Distinct方法或GroupBy方法从Book的ObservableCollection获取不同的值。 But I prefer the first way just because it shorter/simpler in this case. 但是我更喜欢第一种方法,因为在这种情况下它更短/更简单。

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);
}

Then in XAML, bind each ListBox's ItemsSource to corresponding property that have been created in ViewModel : 然后在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}">
        ....

        ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM