Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi,

I'm struggling with some XAML syntax. In this first piece of code, I have attached a command to a button in the button's attributes:

XML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyAssembly.CodeBehindClass">

	<StackLayout>
		<Button x:Name="buttonName" Text="Press" Command="{Binding ButtonOnClickedCommand}" />
	</StackLayout>

</ContentPage>


This works fine. But what if I want to use the alternative syntax as shown below? I can set the text no problem, but I can't figure out how to bind the command:

XML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MyAssembly.CodeBehindClass">

	<StackLayout>
		<Button x:Name="buttonName">
            <Button.Text>Press</Button.Text>
            <Button.Command>STRUGGLING FOR SYNTAX HERE</Button.Command>
	</StackLayout>

</ContentPage>


I think I can express my question more simply as follows:

In general, in XAML, if you have a UI element with a property then you can set the property one of two ways. You can either do this:

XML
<MyControl MyProperty="Value-which-may-be-a-binding-expression"/>


Or you can achieve exactly the same thing like this:

XML
<MyControl>
    <MyControl.MyProperty>Value-goes-here</MyControl.MyProperty>
</MyControl>


I can get this to work with various properties but can't work out how to do it with the Command. But I think my problem is not specifically the Command property; I think it is the fact that I don't know how to write a binding expression using the second syntax above.

Hope this makes sense.



Kind wishes ~ Patrick
Posted
Updated 11-Jan-16 6:58am
v2
Comments
Sergey Alexandrovich Kryukov 11-Jan-16 10:13am    
Why dependency property? Who told you that " break the button over a few lines" is related to the button command?
Aren't you just adding separate buttons? (Why?) But then, what's the problem of adding commands?
—SA
Patrick Skelton 11-Jan-16 11:14am    
Hi, Sergey.

I am simply experimenting with XAML, to try to improve my understanding. I thought that, in general, if you had a UI element with a property then you could set the property one of two ways. You can either do this:

<mycontrol myproperty="Value-which-may-be-a-binding-expression"/>

Or you could achieve exactly the same thing like this:

<mycontrol>
<mycontrol.myproperty>Value-goes-here</mycontrol.myproperty>
</mycontrol>

I can get this to work with various properties but can't work out how to do it with the Button.Command property. But I think my problem is not specifically the Command property; I think it is the fact that I don't know how to write a binding expression using the second syntax above.

My specific question is: how do you write a binding expression where I have written 'Value-goes-here'?

Patrick
Sergey Alexandrovich Kryukov 11-Jan-16 12:32pm    
Thank for your clarification. (But it's still better to put all code in the body of the question, using "Improve question", as you can format it properly there. Not a problem at this moment, the question is clear.)

Your question is valid, but bay I also ask you: why would you use this kind of binding? The standard approach is different: you add to your window.CommandBindings a new instance of CommandBinding. It binds and instance of ICommand with the handler and "can execute" handler. For custom ICommand, you implement it in your custom RoutedUICommand with some text and name (constructor parameters).

Then the text of content of your Command XAML element will be simply the name of this RoutedUICommand (or existing instance of RoutedUICommand, such as ApplicationCommands.*).

For example, <Button.Command>ApplicationCommans.Close</Button.Command>

You can use "{Binding ...}", too, but it's more convenient to write it as an element attribute...

—SA
Patrick Skelton 11-Jan-16 13:17pm    
Thank you for that, Sergey (and also your patience). I am actually using this XAML in a Xamarin.Forms Portable Class Library. I am very new to this, but it appears that not all the features found in WPF have been implemented. As I said, I'm really just experimenting, to try to gain more understanding. I will bear your comments in mind.
Sergey Alexandrovich Kryukov 11-Jan-16 14:25pm    
You are very welcome. As I said before, your question is quite correct (I'll up-vote it); I tried to answer on this syntax, but later saw Solution 2 (please see my comment to it). I'm not sure that "not implemented" is a problem. I'm not quite happy with XAML design itself and WPF architecture, but it certainly works...

As to the practical implementation of commands, I would advise to pay attention for the way I described above. Maybe property binding is not really needed. You just create custom commands and bind them; so all relationships between UI elements' commands/action are shifted to those CommandBinding and handlers, where you don't have any syntactic difficulties. I generally insist on not overdoing XAML, but not everyone agree with me or even understand it.

—SA

1 solution

You just need to add a Binding element, with the same properties as the {Binding ...} markup extension:
XML
<Button x:Name="buttonName">
    <Button.Text>Press</Button.Text>
    <Button.Command>
        <Binding Path="ButtonRelaxOnClickedCommand" />
    </Button.Command>
</Button>

Property Element Syntax : XAML Syntax In Detail[^]
Declaring a Binding in XAML : Binding Declarations Overview[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-16 14:21pm    
My 5. I tested this way, of course, it works.
I just tried to write the similar thing, but Intellisense confused me a bit: it only showed RoutedCommand and RoutedUICommand, nothing else. In VS 2013 and 2015, Intellisense work with binding has been improved, but there is a room for a lot of improvements. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900