Mvvmcross: UserControl DataContext is always null

Created on 22 Mar 2020  路  6Comments  路  Source: MvvmCross/MvvmCross

I made an android app using MvvmCross a while ago and I would like to make a WPF version of it. I followed the TipCalc example in the documentation, however my MvxWpfView never gets its associated ViewModel set as DataContext (it's not even instanciated) and thus my bindings aren't working.

I searched a lot and tried a lot of things, but I can't seem to get it working. I must be missing something.

App.xaml.cs

public partial class App : MvxApplication
{
    protected override void RegisterSetup()
    {
        this.RegisterSetupType<MvxWpfSetup<Core.App>>();
    }
}

CollectionListView.xaml.cs

[MvxViewFor(typeof(CollectionListViewModel))]
public partial class CollectionListView : MvxWpfView<CollectionListViewModel>
{
    public CollectionListView()
    {
        InitializeComponent();
    }
}

MainWindow.xaml

<views:MvxWindow x:Class="Rhea.Windows.Views.MainWindow"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:views="clr-namespace:MvvmCross.Platforms.Wpf.Views;assembly=MvvmCross.Platforms.Wpf"
                 xmlns:collection="clr-namespace:Rhea.Windows.Views.Collection"
                 Style="{DynamicResource FlatWindowStyle}">
    <collection:CollectionListView />
</views:MvxWindow>

MainWindow.xaml.cs

[MvxWindowPresentation(Identifier = nameof(MainWindow), Modal = false)]
public partial class MainWindow : MvxWindow
{
    public MainWindow()
    {
        InitializeComponent();
    }
}

Thank you for your help.

question

All 6 comments

I don't see the place you actually assign the DataContext? Or are you expecting it to automatically somehow be bound because you specified the ViewModel type in the generic MvxWpfView?

I don't see the place you actually assign the DataContext? Or are you expecting it to automatically somehow be bound because you specified the ViewModel type in the generic MvxWpfView?

That should be the reason, although I can't seem to find where it is done in the TipCalc project

https://github.com/MvvmCross/MvvmCross-Samples/blob/master/TipCalc/TipCalc.WPF/Views/TipView.xaml.cs

GitHub
Tutorials and samples for MvvmCross: The .NET MVVM framework for cross-platform solutions. - MvvmCross/MvvmCross-Samples

I've tried adding this to my View class constructor, but IoCProvider is null
ViewModel = Mvx.IoCProvider.IoCConstruct<CollectionListViewModel>();

Or are you expecting it to automatically somehow be bound because you specified the ViewModel type in the generic MvxWpfView?

I thought that's kind of the idea of this library. ;-)

Anyway, how would you solve the following problem:
Let's say I wanna create a WPF application with a classic file menu on top.
````


|File Actions ... |


|Content |
| |


````
The menu should be always visible and there are menu points that should navigate the content.
My idea was to put the menu in an own user control with a view model that has the commands for navigation with the navigation service.

Maybe I overlooked something but I can't figure out how it's supposed to work with the information in the wpf documentation or in the samples. :-(

(Disclaimer: Haven't worked with wpf for 4 years and used other UI frameworks like angular, react or blazor that have a simpler api for navigating/putting components together, so this might be also a handicap.)

So you are saying TipCalc works, but not your own project?

TipCalc works, but is an example with less complexity. (One Page with no navigation, no user control used in xaml or global controls like a menu)

Well it is unclear from what the author has provided what is different in their project.

Was this page helpful?
0 / 5 - 0 ratings