This is a migrated thread and some comments may be shown as answers.

MVVM - how to override Binding to SaveCommand?

3 Answers 246 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Rob Ainscough
Top achievements
Rank 1
Rob Ainscough asked on 05 Jun 2015, 10:37 PM

I'm trying to figure out how to "intercept" the SaveCommand for RadRibbonBackstageItem and respond to the event in my MVVM code.

 

<telerik:RadRichTextBoxRibbonUI IsMinimized="True"  Grid.Row="0" Grid.Column="0" BackstageClippingElement="{Binding ElementName=gridRadRtbParent}" DataContext="{Binding Commands, ElementName=ManageLettersRadRTB}" CollapseThresholdSize="50,50" Title="Letter" TitleBarVisibility="Collapsed" QuickAccessToolBarPosition="BelowRibbon" ApplicationButtonContent="File">
 
    <telerik:RadRichTextBoxRibbonUI.Resources>
        <telerik:FontFamiliesProvider x:Key="FontFamiliesProvider" />
    </telerik:RadRichTextBoxRibbonUI.Resources>
 
    <!-- Menu - File -->
    <telerik:RadRichTextBoxRibbonUI.Backstage>
        <telerik:RadRibbonBackstage >
            <telerik:RadRibbonBackstageItem Header="New" Icon="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/new.png" IsSelectable="false" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding NewDocumentCommand}" />
            <telerik:RadRibbonBackstageItem Header="Open" Icon="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/open.png" IsSelectable="false" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding OpenDocumentCommand}" />
            <telerik:RadRibbonBackstageItem x:Name="FileSave"  Header="Save" Icon="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/save.png" IsSelectable="false" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding SaveCommand}" >

I tried to use SL5 interactivity but in order to make that work I had remove 

"DataContext="{Binding Commands, ElementName=ManageLettersRadRTB}"

from RadRichTextBoxRibbonUI which basically disables all the built in functionality ... not what I want.

 My goal is to respond with my own code when File - Save (which are RadRibbonBackstageItem) is selected from the RadRichTextBoxRibbonUI and not use the default controls Save process.

Is this possible?  If so, could you provide any hints/sample.

Cheers, Rob.

 

3 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 08 Jun 2015, 04:17 PM
Hi Rob,

The easiest way to intercept the save command is by subscribing to the CommandExecuting/CommandExecuted events of RadRichTextBox, but this is not a very MVVM-friendly solution.

The other alternative is to create custom command(s) and set the data context of the backstage (or specific backstage item) to your view model. I'm sending you a simple example with sample implementation for the save command. It also shows how to create a DependencyProperty of type RadRichTextBox and pass it to the custom command.

In the sample I've set the data context of the backstage item to my view model, which may be a bit inconvenient if you plan on creating many custom commands, but is the only solution I could give.
<telerik:RadRibbonBackstageItem Header="Open" Icon="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/open.png" IsSelectable="false" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding OpenDocumentCommand}"/>
<telerik:RadRibbonBackstageItem Header="Save" Icon="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/save.png" IsSelectable="false" DataContext="{Binding ElementName=Commands}" Command="{Binding MySaveCommand}"/>

I hope this helps.

Regards,
Petya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Scott
Top achievements
Rank 2
answered on 22 May 2020, 02:20 AM
Is there an alternative to the event handlers that follows the MVVM design pattern in newer versions since this post?  Not supporting an MVVM model makes it very difficult to hack this into an MVVM application without replacing all the commands entirely.
0
Tanya
Telerik team
answered on 26 May 2020, 10:54 AM

Hello Scott,

The mechanism for the commands hasn't been changed and the approach provided by Petya is still valid. You can store the commands of RadRichTextBox in the view model and change only the one that you need. While I understand that this is not the best option that could be offered, changing the way the commands work might affect many of the users of the control which is something that we strive to avoid.

Regards,
Tanya
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
Tags
RichTextBox
Asked by
Rob Ainscough
Top achievements
Rank 1
Answers by
Petya
Telerik team
Scott
Top achievements
Rank 2
Tanya
Telerik team
Share this question
or