Button vs RadButton

11 posts, 2 answers
  1. Tomas
    Tomas avatar
    92 posts
    Member since:
    Jun 2010

    Posted 12 Apr 2011 Link to this post

    Hi,

    I don't know if this is by design or not but I thought the default Silverlight button and the Telerik RadButton are supposed to work the same, but I've found a case where they're behaving differently.

    I have a button defined like this:

    <Button Content="..." Command="{Binding MyCommand}" IsEnabled="{Binding MyIsEnabledProperty}" />

    MyCommand is of type DelegateCommand but without a CanExecute specified.
    MyIsEnabledProperty is a simple boolean property.

    In this case the button is enabled or disabled depending on the value of MyIsEnabledProperty

    However if I change the button to a RadButton instead like this:

    <telerik:RadButton Content="..." Command="{Binding MyCommand}" IsEnabled="{Binding MyIsEnabledProperty}" />

    Then IsEnabled is not working as I would expect anymore, adding a CanExecute to my DelegateCommand makes it behave as
    expceted again.

    So it looks like the order in which properties are evaluated are differenet between the Button and RadButton.

    Tomas

  2. Answer
    Kiril Stanoev
    Admin
    Kiril Stanoev avatar
    1512 posts

    Posted 14 Apr 2011 Link to this post

    Hello Tomas,

    Thank you for pointing out this inconsistency. Let me put my two cents in the topic. IMHO, mixing the Command and IsEnabled properties of a Button is not a good practice and we strongly advocate against it since the commanding mechanism internally controls the IsEnabled property of the Button. Therefore, a cleaner way will be to use the Command property and take advantage of the CanExecute method to control the IsEnabled property. Take for example the following scenario.

    public class ProductViewModel : INotifyPropertyChanged
    {
        private bool isLoadProperty;
     
        public ProductViewModel()
        {
            this.LoadCommand = new DelegateCommand(this.LoadProducts, this.CanLoadProducts);
        }
     
        public DelegateCommand LoadCommand { get; set; }
     
        private void LoadProducts(object param)
        {
     
        }
     
        private bool CanLoadProducts(object param)
        {
            return this.IsLoadEnabled;
        }
     
        public bool IsLoadEnabled
        {
            get
            {
                return this.isLoadProperty;
            }
            set
            {
                if (this.isLoadProperty != value)
                {
                    this.isLoadProperty = value;
                    this.LoadCommand.InvalidateCanExecute();
                    this.OnPropertyChanged("IsLoadEnabled");
                }
            }
        }
     
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyname));
            }
        }
    }

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Width="200">
        <TextBlock Text="Button" />
        <Button Content="..." Command="{Binding LoadCommand}" />
        <TextBlock Text="RadButton" />
        <telerik:RadButton Content="..." Command="{Binding LoadCommand}" />
        <CheckBox IsChecked="{Binding IsLoadEnabled, Mode=TwoWay}" Content="IsLoadEnabled" />
    </StackPanel>

    Clicking the CheckBox will result in the following outcome. We will further investigate the topic and see why RadButton behaves differently than Button, but again, you should definitely consider not mixing Command and IsEnabled since this might lead to further issues.
    I'm attaching my test project as well. Please take a look at it and let me know what your thoughts are on the subject. I'd be glad to further continue the discussion with you.

    Regards,
    Kiril Stanoev
    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. Suresh
    Suresh avatar
    14 posts
    Member since:
    Jun 2011

    Posted 16 Jun 2011 Link to this post

    I have the same issue with RadButton. I am able to disable the button if it were Button but not the RadButton. I have tried removing the IsEnabled property binding and disabled the command by making ICommand.CanExecute return false but didn't get through. 
  5. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 21 Jun 2011 Link to this post

    Hi Suresh,

    We are aware of this issues and we agree that this behavior is not acceptable in the RadButton. You can track this issue's status here and here. Unfortunately, we cannot provide an estimated time when they will be resolved.

    Best wishes,
    Petar Mladenov
    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. Oliver
    Oliver avatar
    39 posts
    Member since:
    Jun 2010

    Posted 05 Aug 2011 Link to this post

    this is my code in RelayCommand.
    how to change this code for using IsEnable in RadButton?
    please help me.
    -------------------------------------------------------------------------

    Imports System.Net
    Imports System.Windows
    Imports System.Windows.Controls

    Public Class RelayCommand
        Implements ICommand

        Private relayedAction As Action(Of Object)

        Public Sub New(ByVal relayedAction As Action(Of Object))
            Me.relayedAction = relayedAction
        End Sub

        Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
                Return True
        End Function

        Public Event CanExecuteChanged As EventHandler Implements System.Windows.Input.ICommand.CanExecuteChanged

        Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute
            Me.relayedAction.Invoke(parameter)

        End Sub

    End Class
  7. Tomas
    Tomas avatar
    92 posts
    Member since:
    Jun 2010

    Posted 08 Aug 2011 Link to this post

    Hi Kiril,

    Thanks for the reply and please apologize for my late reply. I'm okay with your answer I mostly wanted to give you a heads up as I think I've read somewhere that your controls works as the native silverlight controls and this behviour confused me at first. But your're probably right that you shouldn't mix the Command and IsEnabled.

    Thanks for the explanation.

    Tomas

  8. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 10 Aug 2011 Link to this post

    Hello Tomas,

     @Oliver
    Could you please elaborate more on your scenario, how you use your RadButton and what exactly do you wish to achieve? Please keep in mind that is not good practice to use both CanExecute() and IsEnabled.Thank you for your cooperation.  

    Greetings,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

  9. Jerry T.
    Jerry T. avatar
    118 posts
    Member since:
    Jul 2012

    Posted 28 Oct 2011 Link to this post

    What's the status of the RadButton and IsEnabled?

    The two PITS entries referenced above still show as Open and in the Q3 Beta the IsEnabled still has no effect on a telerik:RadButton

    Jerry
  10. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 02 Nov 2011 Link to this post

    Hi Jerry T.,

     Yes, these bugs are still not resolved in the RadButton. Unfortunately, we cannot promise an estimated date when they will be fixed. Thank you for your understanding. 

    Regards,
    Petar Mladenov
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  11. Khin Sandar
    Khin Sandar avatar
    1 posts
    Member since:
    Feb 2015

    Posted 28 Feb 2015 Link to this post

    Hi,

    How do I add dynamic events  to RadRibbon Buttons in Ribbon View Paint example? I tried to add command in ButtonTemplate but It's not working. But command is working with Add Tab button as in example.

    <DataTemplate x:Key="ButtonTemplate">
                    <telerik:RadRibbonButton Text="{Binding Text}" Size="{Binding Size}" CollapseToSmall="WhenGroupIsSmall"
                            SmallImage="{Binding SmallImage}" LargeImage="{Binding LargeImage}" Command="{Binding AddTab}"/>
                </DataTemplate>

    Thanks in advance!

    Sandra
  12. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 03 Mar 2015 Link to this post

    Hello Khin,

    To add events to controls which are placed in DataTemplates you can use EventToCommand patters. Its isdescribed in the following help article.

     Event To Command Behavior

    Let us know if this helps you move forward.

    Regards,
    Petar Mladenov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
DevCraft banner