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

RadButton IsIsEnabled Property and Prism Commanding

7 Answers 328 Views
Buttons
This is a migrated thread and some comments may be shown as answers.
Calvin
Top achievements
Rank 1
Calvin asked on 25 Jun 2010, 10:32 PM
I have a button which uses a Prism DelegateCommand to send the Click Event to my ViewModel.  However, when I attempt to bind the IsEnabled property of my button to another ViewModel property, the button does not reflect the status of this ViewModel property.

To test this I attempted to simply set the IsEnabled property to False (instead of binding) but the button remained Enabled.  I then removed the DelegateCommand binding and the button became disabled (when the IsEnabled property was set to False).  This obviously indicates a conflict between the DelegateCommand and the button's IsEnabled property. 

Can you suggest a work around.  Thanks. 

Here are the relevant portions of my code:


7 Answers, 1 is accepted

Sort by
0
Miro Miroslavov
Telerik team
answered on 30 Jun 2010, 01:01 PM
Hi Calvin,

We are aware of this problem, but at the moment I don't think we can workaround it.
If you can wait for the next internal build, it will be fixed.
Thank you for your patience.

Regards,
Miro Miroslavov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Calvin
Top achievements
Rank 1
answered on 08 Jul 2010, 11:01 PM
Any projection regarding the timing of this next internal build?  Thanks.
0
Tina Stancheva
Telerik team
answered on 14 Jul 2010, 12:52 PM
Hi Calvin,

Please accept my apology for the delayed response and for the misunderstanding. The issue you are facing is not a bug since it is the expected behavior when using CAL.

The Click.Command attached property provided by Composite Application Guidance for Silverlight requires you to specify an ICommand (generally using DelegateCommand class). In that command you define an Execute and CanExecute method. The CAL relies on this latter method to check if the command can be executed and sets the control's IsEnabled property to the value returned by the CanExecute method. That is why when you set the IsEnabled property in XAML, it gets overriden with the command's value.

So, when you bound a command to a control the beheaviour is as follows (independently from the value you set to the IsEnabled property in XAML):

  • If the command's CanExecute method returns false: the control will be disabled.
  • If the command's CanExecute method returns true: the control will be enabled.

So, when you use CAL's Click attached property to define commands, the IsEnabled property is set at runtime by the command behavior (more precisely in the UpdateEnabledState method of the CommandBehaviorBase class) which is executed after the isEnabled set in XAML.
protected virtual void UpdateEnabledState()
{
    ...
    if (this.Command != null)
    {
        TargetObject.IsEnabled = this.Command.CanExecute(this.CommandParameter);
    }
}

You can use the RaiseCanExecuteChanged method from the DelegateCommand class to reevalute the CanExecute method and that will update the isEnabled property of all attached controls.

I hope the information will help you. Let us know if we can further assist you.

Greetings,
Tina Stancheva
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Michael
Top achievements
Rank 1
answered on 06 Jan 2011, 07:12 PM
I'm having the same problem, but with binding to the RadRichTextBox.Commands (CutCommand, CopyCommand, PasteCommand, etc).  I tried your solution for the DelegateCommands, and I can't seem to get it to work.  I noticed the CanExecuteOverride method, can I override the CanExecute method?
0
Tina Stancheva
Telerik team
answered on 11 Jan 2011, 04:31 PM
Hello Calvin,

Can you please elaborate on your scenario and what you need to implement?

By design you can use the RadRichTextBox commands like Cut, Copy, Paste to manipulate the text entered in the RadRichTextBox, and therefore in order to enable, for example the Cut and Copy buttons, you will need to select text to be cut/copied, otherwise the buttons will be disabled as there will be no target text to be manipulated. The same goes for the Paste button, if the RichTextBox is read-only, then the Paste button will be disabled.

So in this case the IsEnabled property of the buttons is defined by the target of the Command - whether there is such target or not.

I attached a sample project illustrating the basic implementation of a the RichTextBox commands, so please have a look at it and let me know if it works for you or if you need more info.

All the best,
Tina Stancheva
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Michael
Top achievements
Rank 1
answered on 11 Jan 2011, 04:48 PM
Sorry If I was unclear, I'm not Calvin, I didn't start this thread, but have a similar question.  My name is Michael.

For my page, I'm using a radrichtextbox and binding it's commands to radmenu items.  However, the page is sometimes readonly, depending on how the user gets to it.  When it is readonly, I need all of the commands to be disabled always.  When it is not readonly, I need the commands to be enabled only if the last text box to be in focus was the radrichtextbox  (I have other text boxes on the page, but non of them should can format).  I have this working through an OnMenuOpen event except for the very first time the menu opens.  On the first open, it runs the OnMenuOpen event, but after that event has run it sets the enabled property of the menu items to the CanExecute of the commands.  I need a way to set the enabled/disabled property of the buttons (or a way to overwrite the canexecute function in the commands) so that I can take into consideration the readonly boolean and the last text box in focus.  At very worste, I may be able to add or remove the bindings in the OnMenuOpened event, but that seems a bit hacky.
0
Iva Toteva
Telerik team
answered on 14 Jan 2011, 04:45 PM
Hi Michael,

The formatting commands are by default disabled in read-only mode.
In order to disable the execution of some of the other commands when RadRichTextBox is in read-only mode, you can create your custom commands. An example of how this can be done for the SaveCommand is shown below:

public class CustomSaveCommand : SaveCommand
{
    public CustomSaveCommand(RadRichTextBox editor)
        : base(editor)
    {
    }
  
    protected override bool CanExecuteInReadOnlyMode
    {
        get
        {
            return false;
        }
    }
}

Next, set the command to the desired button trough the attached property (don't forget to remove the XAML that sets the original command):
RadRichTextBoxRibbonUI.SetRichTextCommand(this.buttonSave, new CustomSaveCommand(this.radRichTextBox1));

This can be set in XAML too, if you have appropriate source to bind to (one that exposes CustomSaveCommand property):
<telerik:RadRibbonButton Name="buttonSave" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/save.png" Text="Save"  telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding CustomSaveCommand}" />
and in code-behind:
this.buttonSave.DataContext = this;

Don't hesitate to contact us if you need more help on this.

Kind regards,
Iva
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
Buttons
Asked by
Calvin
Top achievements
Rank 1
Answers by
Miro Miroslavov
Telerik team
Calvin
Top achievements
Rank 1
Tina Stancheva
Telerik team
Michael
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or