Proper way to bind to ViewModel and also use page.cs file

GaganSinghGaganSingh USUniversity ✭✭✭

I set up my project using this MVVM Light example
How do i Bind to an ICommand from the XAML to the XAML code-behind while i have BindingContext = new SomeViewModel().

Sometimes i need to access page controls from XAML and i cant do that from the ViewModel so i need to do it from the XAML code-behind.
But now this creates messy code.

Is there a more proper way to structure this and have the 3 files talk to each other effortlessly?

Answers

  • JulienRosenJulienRosen CAMember ✭✭✭✭
    edited April 2016

    You don't bind to the code behind of your view. You bind to a view model. Your command should exist in your view model.

    Sometimes i need to access page controls from XAML and i cant do that from the ViewModel

    Most of the time, this is incorrect. Your design can most likely be improved to not need this access.

  • GaganSinghGaganSingh USUniversity ✭✭✭

    I am binding to the viewmodel. What if i need to add controls programmatically? and i need to bind the commands to those controls? or i need to set properties in those controls. How do i fetch those controls in Xaml or the code behind and modify them in the viewmodel?

Sign In or Register to comment.