Aryan M
Aryan M

Reputation: 667

How to bind a view model property to a XAML control

Please let me know if Ill be able to bind a property value of a view model to a XAML control.

XAML:

<Emtry x:Key="addressLine1" />

ViewModel:

public string addressLine1 { get; set; }

Is it possible to create a two way binding?

Upvotes: 0

Views: 493

Answers (2)

Sujith Kumar
Sujith Kumar

Reputation: 51

<Entry x:Name="entAddress" Text="{Binding addressLine1}"/>

Upvotes: 1

Gerald Versluis
Gerald Versluis

Reputation: 34118

You will have to do it like this: <Entry Text="{Binding addressLine1, Mode=TwoWay}" />

The x:Key doesn't have much to do with it. You will have to bind to the property of the control that you want to use. In this case, on the Entry you want to bind it to the Text property so you can show it to the user and the user can edit it.

Then with the notation of {Binding addressLine1, Mode=TwoWay} you specify which property of the view model to bind to and what the mode should be. You can leave the mode out, then it will have the default value which is OneWay most of the time.

To make the connection between the XAML and the view model, you will still have to specify the DataBinding property on the code-behind of the XAML page.

Upvotes: 2

Related Questions