使用不带ViewSortHintAttribute的通用视图模型进行Prism区域排序

时间:2013-10-01 17:23:31

标签: c# wpf prism

我有一个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的构造函数而不是依赖属性?

1 个答案:

答案 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属性。

您可以在以下棱镜指南章节中找到更多信息:

希望这有帮助。