我有一个PRISM地区:
<ItemsControl prism:RegionManager.RegionName="{x:Static inf:RegionNames.AdministrationCommandsRegion}">
<ItemsControl.ItemTemplate>
...
</ItemsControl.ItemTemplate>
</ItemsControl>
我正在使用区域管理器添加视图模型:
_regionManager.Regions[RegionNames.AdministrationCommandsRegion].Add(new CommandViewModel("User Management", new DelegateCommand(RequestNavigate));
CommandViewModel
看起来像这样:
public class CommandViewModel
{
public CommandViewModel(string displayName, ICommand command)
{
if (command == null) throw new ArgumentNullException("command");
DisplayName = displayName;
Command = command;
}
public string DisplayName { get; private set; }
public ICommand Command { get; private set; }
}
我想指定区域中CommandViewModels
的排序,但我找不到为ViewSortHint
指定CommandViewModel
属性的方法,因此每个属性都有所不同实例。有什么方法可以将ViewSortHint传递给CommandViewModel
的构造函数而不是依赖属性?
答案 0 :(得分:3)
您可以使用ViewSortHint
Region的属性解决排序问题,而不是使用SortComparison
属性。
可以将SortComparison
属性设置为Comparison<object>
委托方法,以便对 ViewModels 进行排序。
this._regionManager.Regions["MyRegion"].SortComparison = CompareViewModels;
此比较可以在SortIndex
属性上进行,例如,在相关的 ViewModels 上实现ISortable
接口。因此,委托方法会比较ISortable
SortIndex
属性:
private static int CompareViewModels(object x, object y)
{
ISortable xSortable = (ISortable) x;
ISortable ySortable = (ISortable) y;
return xSortable.SortIndex.CompareTo(ySortable.SortIndex);
}
最后,您可以将SortIndex
值传递给ViewModel
构造函数,并为每个实例设置ISortable
属性。
您可以在以下棱镜指南章节中找到更多信息:
希望这有帮助。