The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
UWP: DataContext null when using Path and Source Binding RRS feed

  • Question

  • Since Visual Studio 2015 is in RC status I'm not sure if I've found a bug or I'm doing something wrong with my project.

    I've created a Viewmodel Locator with MVVM Light and exposed static properties that return the instance of each viewmodel. The locator is registered as a static resource in the app.xaml.

    Usually I'll bind this in the Page Xaml with a line like this:

    DataContext="{Binding Path=MyViewModel, Source={StaticResource ViewModelLocator}}"

    However, while I've never had a problem with this working before trying it in VS2015, it is not working in my new project that I am creating for Windows 10.

    When I run the project, DataContext is null in the Page, even though it is clearly assigned in the XAML as I've done with dozens of other projects (on vs2013, win/phone 8.1). I checked at run time and the ViewModelLocator static resource is created, initialized, and its MyViewModel property also contains the ViewModel I want. so there's definitely no null references in the VM or the Locator, but for some reason that XAML declaration that binds it to the DataContext of the page appears to be ignored...

    I set a break point in the static property getter for MyViewModel in the locator and it is never called...

    So I'm not sure if this is a problem with VS2015, MVVM Light, Windows 10 SDK, or Windows 10 itself (current build 10162)...

    I tried to open the project in vs2013 to confirm it's a bug but I can't seem to get that to work with my UWP project even after installing the tools...

    has anyone else experienced this problem?


    Sunday, July 5, 2015 4:18 PM

Answers

  • I found the problem. the properties to get the individual viewmodels in the locator should NOT be static, and in fact looking at my other projects they never have been.

    I'm not sure why I made them static in this case but removing that keyword from the get in the locator immediately fixed the problem.

    Sunday, July 5, 2015 4:47 PM

All replies

  • I found the problem. the properties to get the individual viewmodels in the locator should NOT be static, and in fact looking at my other projects they never have been.

    I'm not sure why I made them static in this case but removing that keyword from the get in the locator immediately fixed the problem.

    Sunday, July 5, 2015 4:47 PM
  • Hi SelAromDotNet,

    Thanks for sharing the answer. Others who meet the same problem will benefit from your answer.

    Best regards,



    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, July 6, 2015 7:51 AM