Forum Xamarin Xamarin.Forms

MVVM without xaml (binding issues)

ysteinSelbekkysteinSelbekk NOMember, University

Hi, I'm playing around with some code, trying to achieve the same thing with what I have already done with xaml, but now without it. So first of all I have simply created two .cs files one customersViewP.cs and the other customersViewP.gui.cs. These are partial classes implementing the ContentPage class. The gui file has only the InitializeComponent function implemented where all gui code takes place. The customersViewP.cs should be a complete copy of the code-behind file customersView.xaml.cs.

That being said I have a customersViewModel.cs which both of these implementations (xaml and not xaml) should use, and it is here I have encountered into binding issues for the non-xaml implementation. I am asking as I have found poor and little examples for this.

The code-behind (customersView.xaml.cs) and customersViewP.cs have the following constructor:


Lets being with the viewModel (CustomersViewModel):


These are the properties I have bound in the xaml version like this


So how do I bind these with code? I have managed to use the event handler instead of the command property,
but out of curiosity, how does one bind a command in code to get this to work?


I have also a dataTemplate that looks like this in xaml (dxGrid is part of devexpress gridcontrol)


and like this in code, which I havn't got to test as I have not managed to get the binding to work.


Best Answer

Answers

  • ysteinSelbekkysteinSelbekk NOMember, University

    Thanks :) That guided me in the right direction. However I use new Command instead of Action.


    Now I have problem with the DataTemplate which I bind in xaml like this.


    How should I reference that Source binding in code? I have tried the following with no luck, and I get this error


    As I understand it, the Source binding is referencing the ItemsSource Binding. It is to access
    each items FirstName and Email variables.


  • JamesRussoJamesRusso USMember

    You're receiving the "Object Reference Error" because there is no property on your viewmodel called "Source". You really don't need to set the BindingContext of the Grid since it is essentially the row of the ListView, so you're other bindings should work as long as they are properties on your "Customer" model.

    For more information check out this link here by @JamesMontemagno and checkout this link at the Xamarin Documentation.

  • ysteinSelbekkysteinSelbekk NOMember, University

    Hi, thanks for your input, although I created another (hopefully a more shorter/direct) question about this.
    forums.xamarin.com/discussion/46970/binding-in-code#latest

    I'm using devexpress and the Source binding works in xaml. Their example code can be found here.
    https://documentation.devexpress.com/#Xamarin/clsDevExpressMobileDataGridTemplateColumntopic

    The problem was to do this in code. I have Finally figured it out though :) code is shown in the other thread.

Sign In or Register to comment.