c# - 使用不带 ViewSortHintAttribute 的通用 View 模型进行 Prism 区域排序

标签 c# wpf prism

我有一个 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);
}

最后,你可以通过SortIndexViewModel构造函数并设置 ISortable每个实例的属性。

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

希望这有帮助。

关于c# - 使用不带 ViewSortHintAttribute 的通用 View 模型进行 Prism 区域排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19122272/

相关文章:

c# - 为什么返回的 DataTable 在 FileHelpers 中有只读列

c# - IXmlSerialized 根据架构进行验证

c# - 在 VS 2013 SPA AccountController 上使用 EF Code-first 扩展 IdentityUser

c# - TLS 1. 2 客户端和服务器无法通信,因为它们不具备通用算法

c# - 在 XAML 中正确设置绑定(bind)源

c# - 在显示两列的 WPF 组合框中搜索

wpf - MVVM - 重用具有多个 ViewModel 的 View

c# - 只比较 DateTime C# 中的日期

wpf - 根据用户权限向区域注册 View

silverlight-4.0 - Prism 。当我使用 RegisterViewWithRegion 显示我的 View 时,OnNavigationTo 不会触发