我有一个 PRISM 区域:
<ItemsControl prism:RegionManager.RegionName="{x:Static inf:RegionNames.AdministrationCommandsRegion}">
<ItemsControl.ItemTemplate>
...
</ItemsControl.ItemTemplate>
</ItemsControl>
我正在使用区域管理器添加 View 模型:
_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
的顺序,但找不到为 CommandViewModel
指定 ViewSortHint
属性的方法code> 以便每个实例都不同。有什么方法可以将 ViewSortHint 传递到 CommandViewModel
的构造函数中,而不是依赖属性?
最佳答案
而不是使用 ViewSortHint
属性,您可以使用 SortComparison
解决排序问题地区的属性(property)。
SortComparison
属性可以设置为 Comparison<object>
委托(delegate)方法以便对 ViewModels 进行排序。
this._regionManager.Regions["MyRegion"].SortComparison = CompareViewModels;
可以在 SortIndex
上进行此比较属性实现例如 ISortable
相关ViewModels上的接口(interface)。因此,委托(delegate)方法将比较 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
每个实例的属性。
您可以在以下 Prism 指南章节中找到更多信息:
希望这有帮助。
关于c# - 使用不带 ViewSortHintAttribute 的通用 View 模型进行 Prism 区域排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19122272/