C# 如何在已排序的silverlight树状视图中维护当前选择?
我有一个silverlight 4C# 如何在已排序的silverlight树状视图中维护当前选择?,c#,silverlight,sorting,treeview,collectionviewsource,C#,Silverlight,Sorting,Treeview,Collectionviewsource,我有一个silverlight 4TreeView控件,它具有数据层次结构。我希望每个级别中的项目按字母顺序排序,因此我使用的是CollectionViewSource,但实际上并不关心如何完成排序 CollectionViewSource似乎观察到CollectionChanged事件,因此在添加和删除项目时,排序工作正常 CollectionViewSource没有观察到正在排序的属性的更改,因此当项目的文本更改时,不会保持排序。调用CollectionViewSource.View.Ref
TreeView
控件,它具有数据层次结构。我希望每个级别中的项目按字母顺序排序,因此我使用的是CollectionViewSource
,但实际上并不关心如何完成排序
CollectionViewSource
似乎观察到CollectionChanged
事件,因此在添加和删除项目时,排序工作正常
CollectionViewSource
没有观察到正在排序的属性的更改,因此当项目的文本更改时,不会保持排序。调用CollectionViewSource.View.Refresh()
对列表重新排序,但放弃所选内容。如何保持TreeView
selection而不丢失和重新设置selection
示例项目:
说明:
此项目将创建项目的单级树。每个项目都有一个项目编号和一个数字前缀,以使排序实际执行一些有趣的操作。按钮将添加项目、删除最旧的项目和重命名最旧的项目
构建示例:
- 创建名为“SortTest”的新silverlight应用程序
- 添加对System.Windows.Controls的引用(用于树视图)
- 更新以下文件:
- 添加和删除项目时,将保留当前选择
- 重命名项时(从
中调用OnRenameButtonClick()
时),当前选择将丢失Refresh()
- 如果删除了对
的调用,则重命名项目时会保留选择,但不会重新排序列表以说明名称更改Refresh()
<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); }
}
}
}
使用System.Windows.Controls;
使用System.Collections.ObjectModel;
使用System.Windows;
使用System.Windows.Data;
使用系统组件模型;
使用制度;
使用System.Collections.Specialized;
名称空间排序集
{
公共部分类主页面:UserControl
{
私有ObservableCollection项=新ObservableCollection();
private CollectionViewSource sortedItems=新的CollectionViewSource();
私有int itemNumber=1;
公共主页()
{
sortedItems.Source=项目;
sortedItems.SortDescriptions.Add(新的SortDescription(“Name”,ListSortDirection.升序));
DataContext=this;
初始化组件();
}
公共ICollectionView项{get{return sortedItems.View;}}
在指定按钮上的私有无效单击(对象发送方,路由目标)
{
ItemViewModel item=新的ItemViewModel();
item.Name=DateTime.Now.millis秒.ToString(“D3”)+“item#”+itemNumber;
itemNumber++;
项目。添加(项目);
}
私有void on remove按钮单击(对象发送方,路由目标)
{
如果(items.Count>0)
{
项目移除(0);
}
}
私有void OnRenameButtonClick(对象发送方,RoutedEventArgs e)
{
如果(items.Count>0)
{
items[0].Name=DateTime.Now.millis秒.ToString(“D3”)+items[0].Name.Substring(3);
sortedItems.View.Refresh();
}
}
}
公共类ItemViewModel:DependencyObject
{
public static dependencProperty NameProperty=dependencProperty.Register(“名称”、typeof(字符串)、typeof(ItemViewModel)、null);
公共字符串名
{
获取{返回GetValue(NameProperty)作为字符串;}
set{SetValue(NameProperty,value);}
}
}
}
谢谢 将所选项目保留到隔离数据存储中,并在重新加载treeview后重新选择它。将所选项目保留到隔离数据存储中,并在重新加载treeview后重新选择它