【问题标题】:How to Sort a ListBox of Images by Date如何按日期对图像列表框进行排序
【发布时间】:2013-06-18 02:58:18
【问题描述】:

我将 MainPage 中的 ListBox 绑定到使用 CameraCaptureTask 拍摄的图像集合。一切正常,尽管我希望能够在我的 SettingsPage 中的各个 RadioButtons 被选中时将排序顺序从升序更改为降序。我在 IsolatedStorage 中创建了一个值,它会记住选中了哪个 RadioButton,这样当我的应用程序的 MainPage 加载时,ListBox 的绑定集合将被相应地排序和显示。然而,我的收藏品的实际排序是我遇到问题的地方。注意,集合中的每张图片,一个DateTaken属性也被保存了。

MainPage.xaml

<ListBox x:Name="Recent" ItemsSource="{Binding Pictures}" Margin="8" 
                     SelectionChanged="recent_SelectionChanged"
</ListBox>

现在,在我的构造函数中,我将DataContext 设置为等于PictureRepository.Instance,它实际上填充了来自IsolatedStorage 的图像。我不确定在绑定之前在哪里或如何更改集合的排序顺序。我在想,事实上我可能想要绑定一个排序列表的副本,实际上并没有改变 IsolatedStorage 中的排序顺序。我试图做以下从Sorting Listbox Items by DateTime values 引用的事情

MainPage.xaml.cs

public MainPage()
    {
        InitializeComponent();

        DataContext = PictureRepository.Instance;
        //Determine which Sort Radio Button has been Checked and display collection accordingly
        //Also not sure if this should be performed in the OnNavigatedTo event
        if (Settings.AscendingSort.Value)
        {
            //PictureRepository.Instance.Pictures.OrderBy(p => p.DateTaken).First();
            //DataContext = PictureRepository.Instance;
            var items = Recent.Items.Cast<CapturedPicture>().OrderBy(p => p.DateTaken).ToArray();
            if (Recent.Items.Count != 0)
                Recent.Items.Clear();
            Recent.Items.Add(items);
        }
        else
        {
            //PictureRepository.Instance.Pictures.OrderByDescending(p => p.DateTaken).First();
            //DataContext = PictureRepository.Instance;
            var items = Recent.Items.Cast<CapturedPicture>().OrderByDescending(p => p.DateTaken).ToArray();
            Recent.Items.Clear();
            Recent.Items.Add(items);
        }
    }

这两个选项都不起作用,尽管我承认我之前从未尝试过在填充 ListBox 之前对 ObservableCollection 进行排序。在学习这个概念时,任何链接、帮助或建议都将不胜感激!

【问题讨论】:

  • 您的 DataContext 是“PictureRepository.Instance”,您的 ListBox 绑定到“Pictures”,并且您正在对名为“Recent”的属性进行排序。您在什么时候尝试将 ListBox 的 ItemsSource 设置为排序项?
  • 我想根据在 SettingsPage 中选择的 RadioButton 立即设置排序的项目。 PictureRepository.Instance 从模型返回 Pictures ObservableCollection,Recent 是集合绑定到的 ListBox。

标签: c# windows-phone-7 sorting windows-phone-8 observablecollection


【解决方案1】:

在对 ListBox 进行排序时,我更喜欢使用 CollectionViewSource。您无需更改要绑定到的后端集合,而是允许控件处理此问题。

您的页面 xaml:

<phone:PhoneApplicationPage.Resources>
    <CollectionViewSource x:Key="PicturesViewSource" Source="{Binding Pictures}">
        <!-- Add for design time help. This object should return a collection of pictures
        <d:Source>
            <viewModels:MyFakeObject/>
        </d:Source>
        -->
    </CollectionViewSource>
</phone:PhoneApplicationPage.Resources>

<Grid>
    <ListBox ItemsSource="{Binding Source={StaticResource PicturesViewSource}}"/>
</Grid>

在您的页面中,您可以通过添加或删除SortDescriptions 来修改ColletionViewSource 的排序方式。每当用户更改单选按钮时,您都会这样做。

PicturesViewSource.SortDescriptions.Clear();
PicturesViewSource.SortDescriptions.Add(new SortDescription("DateTaken", ListSortDirection.Descending));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2012-12-22
    • 2020-12-22
    • 2011-09-08
    • 1970-01-01
    相关资源
    最近更新 更多