WPF button Command vs Content

Topics: Bugs, UI Architecture
Jan 9, 2014 at 10:38 AM
Edited Jan 9, 2014 at 10:40 AM
I have a bug related with the use of commands in buttons in WPF.

I have the following code
<Button x:Name="CancelCommand" 
            Content="Cancel"
            Command="CancelCommand" ... />
When I render this button the text appearing in the button is something like Framework.UI.RellayCommand (the namespace of the command) instead of the content I define.

Yet, when i withdraw the 'x:Name"CancelCommand"' having
<Button Content="Cancel" 
            Command="CancelCommand" ... />
I have the desired behaviour and text within the button.

Can someone help me clarify if there is some kind of problem regarding the use of Caliburn naming conventions and the use of Commands?

Thx,
lcrsantos
Jan 12, 2014 at 7:00 PM
Edited Jan 12, 2014 at 7:00 PM
<Button x:Name="CancelCommand" Content="Cancel" /> <<Convention will work

<Button Content="Cancel" Command="{Binding CancelCommand}" /> << but you lose some automagic abilities

Is all you will need, convention will take care of wiring up the "CancelCommand" in your viewmodel to the control thru binding that is built into CM. Effectively you have broken the mechanic which your "double" binding of the name to command... Essentially you don't need to set anything to Command at all with conventions that CM already takes care of with smoke and mirrors for you.

Aside from that also be thinking of guard properties if necessary probably not necessary in this case, but creating a bool property with "Can" as part of the prefix of the property tells CM hey we have something to check before we are allowing the button to be enabled...

e.g. CanCancelCommand , CanSubmit, etc.


hth

Morgan
Marked as answer by lcrsantos on 3/6/2014 at 7:21 AM
Mar 6, 2014 at 3:21 PM
Thanks Morgan that was what I was kinda expecting yet I was not sure! I will revert my fix to your format.

About guarding the properties I already have the habit of doing that to ensure there are not mismatches within workflow of operations but i just not thought necessary to represent in the example above.

Again thanks
Luis