【发布时间】: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从模型返回PicturesObservableCollection,Recent是集合绑定到的 ListBox。
标签: c# windows-phone-7 sorting windows-phone-8 observablecollection