2

我正在分析一个使用 Catel 作为 MVVM 框架的 WPF 应用程序,我已经看到在 ViewModel 中我有 2 个保留路径 在此处输入图像描述

现在我在附加到上下文菜单的行为中创建了这样的 RadMenuItem,它们被定义为

protected virtual IEnumerable<RadMenuItem> GetRowMenuItems(RadContextMenu contextMenu)
    {
        var rowItems = new List<RadMenuItem>();

        RadMenuItem saveSettings = new RadMenuItem
        {
            Tag = "force",
            Header = CoreResources.LBL_SAVE_SETTINGS,
            Command = DefaultRtViewContextMenuCommands.SaveLayoutDataCommand,
            CommandParameter = AssociatedObject,

            Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/IF.Tesoreria.Client.WPF.Core;component/Media/save.png")) }
        };

        rowItems.Add(saveSettings);

        RadMenuItem loadSettings = new RadMenuItem
        {
            Tag = "force",
            Header = CoreResources.LBL_LOAD_SETTINGS,
            Command = DefaultRtViewContextMenuCommands.LoadLayoutDataCommand,
            CommandParameter = AssociatedObject,
            Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/IF.Tesoreria.Client.WPF.Core;component/Media/load.png")) }
        };

现在命令定义在

    public class DefaultRtViewContextMenuCommands
{
   public static Command<FlexGridCommandParameter> SaveLayoutDataCommand { get; private set; }
    public static Command<FlexGridCommandParameter> LoadLayoutDataCommand { get; private set; }

    private static void OnLoadLayoutCommandExecute(FlexGridCommandParameter grid)
    {
        Argument.IsNotNull(() => grid);

        var dependencyResolver = DependencyResolverManager.Default;
        var openFileService = dependencyResolver.DefaultDependencyResolver.Resolve<IOpenFileService>();
        openFileService.Filter = "Gridview setting file|*.flexgrid";
        if (openFileService.DetermineFile())
        {
            // User selected a file
            using (var fs = File.OpenRead(openFileService.FileName))
            {
                GridViewPersistenceHelper.LoadLayout(grid.Grid, fs, null);
            }
        }
    }

    private static void OnSaveLayoutCommandExecute(FlexGridCommandParameter grid)
    {
        Argument.IsNotNull(() => grid);

        var dependencyResolver = DependencyResolverManager.Default;
        var saveFileService = dependencyResolver.DefaultDependencyResolver.Resolve<ISaveFileService>();
        saveFileService.Filter = "Gridview setting file|*.flexgrid";
        if (saveFileService.DetermineFile())
        {
            // User selected a file
            using (var fs = File.OpenWrite(saveFileService.FileName))
            {
                GridViewPersistenceHelper.SaveLayout(grid.Grid, fs);
            }
        }
    }

 static DefaultRtViewContextMenuCommands()
    {
        viewModelFactory = ServiceLocator.Default.ResolveType<IViewModelFactory>();
        portfolioService = ServiceLocator.Default.ResolveType<IPortfoliosService>();
        pkInstrumentsService = ServiceLocator.Default.ResolveType<IPkInstrumentsService>();

        SaveLayoutDataCommand = new Command<FlexGridCommandParameter>(OnSaveLayoutCommandExecute,_=>true);
        LoadLayoutDataCommand = new Command<FlexGridCommandParameter>(OnLoadLayoutCommandExecute,_=>true);
    }

我究竟做错了什么?谢谢

4

1 回答 1

1
radMenuItem.Command = null;

为我工作。可以反编译看看,当你这样做的时候,菜单项会从Command的CanExecuteChanged中注销自己

于 2018-03-15T16:11:04.660 回答