InitializeComponent before set BindingContext or viceversa?

revyrevy ITMember

In the code behind of a Xaml page, which is the correct order?

InitializeComponent();
BindingContext = ThePageViewModel;

or

BindingContext = ThePageViewModel;
InitializeComponent();

Best Answer

Answers

  • revyrevy ITMember

    Got it, thank you!

  • _albertoms_albertoms ✭✭ MXUniversity ✭✭

    @Vulcan said:
    @revy

    Following is correct.

    InitializeComponent();
    BindingContext = ThePageViewModel;

    Hey, @Vulcan

    Could you extend your answer sharing some thoughts about why this is the right way, please?

  • SteveShaw.5557SteveShaw.5557 ✭✭✭ USMember ✭✭✭

    My understanding is that either order works, but that it may be slightly better performance to set BindingContext last as vulcan suggests. OTOH I've done BindingContext first (because I wasn't 100% certain my custom controls handled null context) without any observable problems.

Sign In or Register to comment.