我有一个带有数据层次结构的silverlight 4 TreeView
控件。我希望每个级别的项目按字母顺序排序,因此我使用的是CollectionViewSource
,但实际上并不关心如何完成排序。
CollectionViewSource
似乎会观察CollectionChanged
个事件,因此在添加和删除项目时排序工作正常。
CollectionViewSource
未观察到正在排序的属性的更改,因此当项目的文本更改时,不会保留排序。调用CollectionViewSource.View.Refresh()
重新排序列表,但丢弃选择。如何保持TreeView
选择而不会丢失并重新设置选择?
示例项目:
说明的
此项目创建单级树项目。每个项目都有一个项目编号和一个数字前缀,以使排序实际上做一些有趣的事情。按钮将添加项目,删除最旧的项目,并重命名最旧的项目。
构建示例:
需要注意的行为:
Refresh()
内调用OnRenameButtonClick()
时),当前选择会丢失。Refresh()
的调用,则在重命名项目时会保留选择,但不会对列表进行重新排序以说明名称更改。MainPage.xaml中
<UserControl x:Class="SortTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<UserControl.Resources>
<Style x:Key="expandedStyle" TargetType="sdk:TreeViewItem">
<Setter Property="IsExpanded" Value="true" />
</Style>
<sdk:HierarchicalDataTemplate x:Key="template">
<TextBlock Text="{Binding Name}" />
</sdk:HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Click="OnAddButtonClick">
<TextBlock Text="Add an item" />
</Button>
<Button Click="OnRemoveButtonClick">
<TextBlock Text="Remove lowest numbered item" />
</Button>
<Button Click="OnRenameButtonClick">
<TextBlock Text="Rename lowest numbered item" />
</Button>
</StackPanel>
<sdk:TreeView Grid.Row="1" ItemsSource="{Binding Items}" ItemTemplate="{StaticResource template}" />
</Grid>
</UserControl>
MainPage.xaml.cs中
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using System;
using System.Collections.Specialized;
namespace SortTest
{
public partial class MainPage : UserControl
{
private ObservableCollection<ItemViewModel> items = new ObservableCollection<ItemViewModel>();
private CollectionViewSource sortedItems = new CollectionViewSource();
private int itemNumber = 1;
public MainPage()
{
sortedItems.Source = items;
sortedItems.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
DataContext = this;
InitializeComponent();
}
public ICollectionView Items { get { return sortedItems.View; } }
private void OnAddButtonClick(object sender, RoutedEventArgs e)
{
ItemViewModel item = new ItemViewModel();
item.Name = DateTime.Now.Millisecond.ToString("D3") + " Item #" + itemNumber;
itemNumber++;
items.Add(item);
}
private void OnRemoveButtonClick(object sender, RoutedEventArgs e)
{
if (items.Count > 0)
{
items.RemoveAt(0);
}
}
private void OnRenameButtonClick(object sender, RoutedEventArgs e)
{
if (items.Count > 0)
{
items[0].Name = DateTime.Now.Millisecond.ToString("D3") + items[0].Name.Substring(3);
sortedItems.View.Refresh();
}
}
}
public class ItemViewModel : DependencyObject
{
public static DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(ItemViewModel), null);
public string Name
{
get { return GetValue(NameProperty) as string; }
set { SetValue(NameProperty, value); }
}
}
}
谢谢!
答案 0 :(得分:0)
将所选项目保留在isolatedStorage中,并在重新加载树视图后重新选择它。