在C#中使用带有RadRibbonView按钮的命令而不是XAML

时间:2017-06-07 15:35:51

标签: c# wpf xaml mvvm telerik

我正在努力让MVVM(相当新的)和RadRibbonView一起工作。问题是,我正在使用带有MVVM模型的标准WPF应用程序。

在我以前的XAML文件中。

<Menu>
            <MenuItem Header="{x:Static properties:Resources.FileMenu}">
                <MenuItem Header="{x:Static properties:Resources.FileMenuNewWorkflow}" Command="{Binding Path=NewWorkflowCommand}" InputGestureText="Ctrl+N"/>
                <MenuItem Header="{x:Static properties:Resources.FileMenuNewService}" Command="{Binding Path=NewServiceCommand}" InputGestureText="Shift+Ctrl+N"/>
                <MenuItem Header="{x:Static properties:Resources.FileMenuOpen}" Command="{Binding Path=OpenWorkflowCommand}" InputGestureText="Ctrl+O"/>
                <Separator/>
                <MenuItem Header="{x:Static properties:Resources.FileMenuSave}" Command="{Binding Path=SaveWorkflowCommand}" InputGestureText="Ctrl+S"/>
                <MenuItem Header="{x:Static properties:Resources.FileMenuSaveAs}" Command="{Binding Path=SaveAsWorkflowCommand}"/>
                <MenuItem Header="{x:Static properties:Resources.FileMenuSaveAll}" Command="{Binding Path=SaveAllWorkflowsCommand}" InputGestureText="Shift+Ctrl+S"/>
                <Separator/>
                <MenuItem Header="{x:Static properties:Resources.FileMenuAddReference}" Command="{Binding Path=AddReferenceCommand}"/>
                <Separator/>
                <MenuItem Header="{x:Static properties:Resources.FileMenuClose}" Command="{Binding Path=CloseWorkflowCommand}"/>
                <MenuItem Header="{x:Static properties:Resources.FileMenuCloseAll}" Command="{Binding Path=CloseAllWorkflowsCommand}"/>
                <Separator/>            
        </Menu>

RelayCommand.cs

public class RelayCommand : ICommand
    {
        private readonly Action<object> execute;
        private readonly Predicate<object> canExecute;

        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            this.execute = execute;
            this.canExecute = canExecute;           
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            return this.canExecute == null ? true : this.canExecute(parameter);
        }

        public void Execute(object parameter)
        {
            this.execute(parameter);
        }
    }

正如您所见,命令ID已明确定义。我想为此添加功能区并从telerik WPF UI中选择RibbonView。用MVVM模型挑选Paint。

将菜单转换为功能区

<telerik:RadRibbonView Grid.Row="0" x:Name="ribbonView" ApplicationName="MyApp" ItemsSource="{Binding Tabs}"
                               ApplicationButtonContent="File" Title="{x:Static properties:Resources.RibbonViewTitle}" ItemTemplate="{StaticResource TabTemplate}"
                               SelectedItem="{Binding SelectedTab, Mode=TwoWay}"
                               MinimizeButtonVisibility="Visible" HelpButtonVisibility="Visible">

在MainWindowModelView中定义以下内容。 SplitButtonViewModel继承自ButtonViewModel。

private GroupViewModel GetFilesGroup()
    {
        GroupViewModel fileItems = new GroupViewModel();
        fileItems.Text = "File";
        SplitButtonViewModel newFile = new SplitButtonViewModel();
        newFile.Text = "New";
        newFile.Size = ButtonSize.Large;
        newFile.LargeImage = GetPath("MVVM/new.png");
        fileItems.Buttons.Add(newFile);


        SplitButtonViewModel openFile = new SplitButtonViewModel();
        openFile.Text = "Open";
        openFile.Size = ButtonSize.Large;
        openFile.LargeImage = GetPath("MVVM/open.png");
        fileItems.Buttons.Add(openFile);

        ButtonGroupViewModel buttonsGroup = new ButtonGroupViewModel();
        buttonsGroup.Buttons.Add(GetButton("save", "Save"));
        buttonsGroup.Buttons.Add(GetButton("SaveAll", "Save All"));
        buttonsGroup.Buttons.Add(GetButton("SaveAs", "Save As"));

        fileItems.Buttons.Add(buttonsGroup);
        return fileItems;
    }

来自telerik MVVM功能区示例的当前ButtonViewModel是

    public class ButtonViewModel : ViewModelBase
        {
            private String text;
            private ButtonSize size;
            private string smallImage;
            private string largeImage;
//perhaps work something with this...
            private RelayCommand command;    

            /// <summary>
            ///     Gets or sets Text.
            /// </summary>
            public String Text
            {
                get
                {
                    return this.text;
                }
                set
                {
                    if (this.text != value)
                    {
                        this.text = value;
                        this.OnPropertyChanged("Text");
                    }
                }
            }

            public ButtonSize Size
            {
                get
                {
                    return size;
                }
                set
                {
                    size = value;
                }
            }

            public string SmallImage
            {
                get
                {
                    return smallImage;
                }
                set
                {
                    smallImage = value;
                }
            }

            public string LargeImage
            {
                get
                {
                    return largeImage;
                }
                set
                {
                    largeImage = value;
                }
            }
        }

因此,所有组的创建都在ModelView中。问题是我不知道如何使命令工作。我有一个RelayCommand类来处理命令。

来自以下链接:http://docs.telerik.com/devtools/wpf/controls/radribbonview/how-to/howto-use-commands-with-radribbonview-buttons 是否可以只调用类RelayCommand(基本上与链接中的示例相同),所以我不需要更改太多而只是从上面的函数调用命令?像openFile.Execute()之类的东西。

大多数问题是如何将XAML连接到命令。所有菜单项现在都在C#中,并且需要在那里进行命令定义。

感谢任何帮助。

0 个答案:

没有答案