RadButton IsIsEnabled Property and Prism Commanding

8 posts, 0 answers
  1. Calvin
    Calvin avatar
    12 posts
    Member since:
    Jun 2010

    Posted 25 Jun 2010 Link to this post

    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:


  2. Miro Miroslavov
    Admin
    Miro Miroslavov avatar
    588 posts

    Posted 30 Jun 2010 Link to this post

    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
  3. DevCraft banner
  4. Calvin
    Calvin avatar
    12 posts
    Member since:
    Jun 2010

    Posted 08 Jul 2010 Link to this post

    Any projection regarding the timing of this next internal build?  Thanks.
  5. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 14 Jul 2010 Link to this post

    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
  6. Michael
    Michael avatar
    15 posts
    Member since:
    Sep 2010

    Posted 06 Jan 2011 Link to this post

    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?
  7. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 11 Jan 2011 Link to this post

    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>>
  8. Michael
    Michael avatar
    15 posts
    Member since:
    Sep 2010

    Posted 11 Jan 2011 Link to this post

    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.
  9. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 14 Jan 2011 Link to this post

    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>>
Back to Top
DevCraft banner