如何在Windows Phone 8中过滤可观察的集合?

时间:2014-01-20 12:10:57

标签: windows-phone-8 filter listbox collectionview listcollectionview

我试过这个How to: Filter Data in a View所以我尝试了这种方法,但它对我不起作用,因为Windows Phone 8环境找不到'ListCollectionView'。

我的收藏正在关注。

//My observableCollection
ObservableCollection<ClonedWrapper> dsForProgress = new ObservableCollection<ClonedWrapper >();

//My Function which shows at first time.
private async Task CloudImages()
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
try
{
    List<MainWrapper> serverdata = new List<MainWrappe>();
        serverdata = await ImageUrlAsync();//This function is returning the Deserialize object of images
        if (serverdata.Count != 0)
        {
            foreach (var imgInfo in serverdata)
            {
                string folderPath = "Fagbokforlaget/Books/" + BookInfo.Id.Trim();
                ClonedWrapper item = new ClonedWrapper()
                {
                    //Name, Cover, Info, Title, IsEnableButton, IsVisibleButton, 
                    // IsVisible are the assigned keys to the xaml page
                    Id = imgInfo.Id,
                    Name = imgInfo.Name,
                    Cover = await GetCoverImage(imgInfo),
                    Info = imgInfo.Info,
                    Title = imgInfo.Title,
                    Url = imgInfo.Url,
                    IsEnableButton = "True",
                    IsVisible = "Collapsed",
                    Date = Convert.ToDateTime(imgInfo.Date)
                };
                if (isf.DirectoryExists(folderPath))
                {
                    item.ButtonStatus = "Read";
                        item.IsVisibleBookDeleteButton = "Visible";
                    }
                    else
                    {
                        item.ButtonStatus = "Download";
                        item.IsVisibleBookDeleteButton = "Collapsed";
                    }
                    dsForProgress.Add(item);
                }
            }
            else
            {
                MessageBox.Show("You have no any downloaded books!");
            }
            listCloudImages.ItemsSource = dsForProgress;
    }
        catch (Exception exe)//I am getting exception here on extreme first run in absense of internet.
        {
            MessageBox.Show(exe.Message);
        }
    }

//我的xaml页面如下。它是第一次加载的默认页面。你可以说它是应用程序的面孔。用户下载图像。和buttonStatus转换为Read按钮。所以现在我正在做什么我会在appliationbar中保留两个按钮,一个云下载第二个。在下载按钮单击我想只显示下载的图像。

   <ListBox Name="listCloudImages" Visibility="Visible" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Auto" FontFamily="Segoe UI" FontStyle="Normal" FontWeight="Thin" Margin="0,0,0,50">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <tools:WrapPanel Orientation="Horizontal">
                </tools:WrapPanel>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderThickness="1" Background="#151414" CornerRadius="3" Margin="3" Width="150" TextOptions.DisplayColorEmoji="True" BorderBrush="#1c1b1b">
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">

                            <Image x:Name="imgBookImage" Source="{Binding Cover}" Visibility="Visible" VerticalAlignment="Top" HorizontalAlignment="Center"
                                       Width="80" Height="100" ImageOpened="imgBookImage_ImageOpened"/>
                            <StackPanel Orientation="Horizontal">

                                <TextBlock Visibility="{Binding IsVisible}" Text="{Binding ProgressPercentage}" FontFamily="Segoe UI" FontSize="18" FontWeight="ExtraBold" Foreground="White" Margin="5,0"></TextBlock>
                            </StackPanel>
                        </StackPanel>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="{Binding Title}" FontFamily="Segoe UI" FontSize="13.5" 
                                       Foreground="White" TextTrimming="WordEllipsis"
                                       VerticalAlignment="Top" HorizontalAlignment="Left" 
                                       TextWrapping="Wrap" 
                                       Width="300" Padding="2"/>
                            <TextBlock Text="{Binding Info}" FontSize="13.5" FontFamily="Segoe UI" 
                                       Foreground="White"
                                       VerticalAlignment="Top" HorizontalAlignment="Left" 
                                       TextWrapping="Wrap" 
                                       Width="300" Padding="2"/>
                            <ProgressBar x:Name="downloadProgressBar" Foreground="Green" IsIndeterminate="True" VerticalAlignment="Center"  Width="120" TextOptions.TextHintingMode="Animated" Visibility="{Binding IsVisible}"  CharacterSpacing="2"/>
                            <Button Content="{Binding ButtonStatus}" x:Name="btnDownload" IsEnabled="{Binding IsEnableButton, Converter={StaticResource ButtonVisibilityIsEnableConverter}}" 
                                       Click="btnDownload_Click" Tag="{Binding}" Width="120" BorderThickness="1" FontSize="13.5" Margin="0,5" 
                                       FontFamily="Segoe UI" tools:SlideInEffect.LineIndex="2" HorizontalAlignment="Left" VerticalAlignment="Top" 
                                       Foreground="White">
                            </Button>

                            <Image x:Name="imgCancelImage" Source="/Assets/Tiles/CancelImage.png" Visibility="{Binding IsVisible}" VerticalAlignment="Center" Margin="97,-66,0,0"
                                         Tap="imgCancelImage_Tap"  HorizontalAlignment="Right" Width="25" Height="25" Tag="{Binding}"/>
                            <Button x:Name="btnDeleteImage" Click="btnDeleteImage_Click"
                                        Tag="{Binding}" BorderThickness="1" Margin="97,-66,0,0" 
                                        Height="55" Width="55"
                                        Visibility="{Binding ButtonStatus, Converter={StaticResource DeleteButtonVisibilityConverter}}">
                                <Button.Background>
                                    <ImageBrush ImageSource="/Images/delete.png" Stretch="Fill"></ImageBrush>
                                </Button.Background>
                            </Button>
                        </StackPanel>
                    </StackPanel>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

3 个答案:

答案 0 :(得分:1)

在您的图书模型中,您可以传递bool属性IsDownload,当您下载图书时,您将此属性设置为true。对于您的过滤器,只需使用linQ获取所有图书下载或没有这样的下载:

var noDownloadedBooks = 
            from b in yourObservableCollection
            where b.IsDownload == false 
            select b; 
var downloadedBooks = 
                from b in yourObservableCollection
                where b.IsDownload == true
                select b; 

答案 1 :(得分:1)

根据您的代码,您可以轻松地做一些事情,如果您只想显示已下载的书籍,请尝试以下方法:

private void ApplicationBarButtonShowAllDownLoaded(object sender, EventArgs e)
    {        
        var c = (Application.Current as App).dsForProgress.Where(x => x.ButtonStatus.Equals("Download"));
        listCloudImages.ItemsSource = c;
    }

确保ObservableCollection<ClonedWrapper> dsForProgress是公开的

答案 2 :(得分:0)

您需要做的是使用CollectionViewSource,它可以指向您的原始集合,它可以处理过滤! Scott Hanselman在这里有一个很好的博客文章示例... http://www.hanselman.com/blog/CollectionViewSourceIsCrazyUsefulForBindingToFilteredObservableCollectionsOnWindowsPhone8.aspx

相关问题