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
10 Answers, 1 is accepted
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
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
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
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
@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.
Petar Mladenov
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>
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
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 >>
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
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.