Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: WPF User-controls MVVM Binding , +
In order to enable drag-drop properly between two user controls, I need to call their viewmodels from the MainWindow viewmodel
I had thought that it would be as simple as this:
<UserControl x:Class="LookupTermWizard.View.Properties"
        Height="300" Width="500" removed="LightGray" DataContext="{Binding}" >
<Window x:Class="LookupTermWizard.MainWindow"
        Title="Lookup Term Wizard" Height="900" Width="1300">
        <vm:MainWindowViewModel />
        <StackPanel DockPanel.Dock="Left" VerticalAlignment="Top" Height="775" >
                <v:Properties DataContext="{Binding PropertiesVM}" />
And then instantiate the viewmodel for the user control from the MainWindowViewModel
        PropertiesViewModel propertiesVM;
        PropertiesViewModel PropertiesVM
            get { return propertiesVM; }
                propertiesVM = value;
        public MainWindowViewModel()
            PropertiesVM = new PropertiesViewModel();
Of course, it doesn't work that way. Any chance of getting some guidance?
Posted 21-Sep-12 8:30am
Matt T Heffron at 21-Sep-12 18:23pm
So what is going wrong? Binding failing?
(I noticed that PropertiesVM is not shown here as public...)
You shouldn't use the DataContext={Binding} in the Properties XAML. The DataContext has been explicitly set in the MainWindow.
(Also, since v:Properties is a UserControl, why wrap it in ContentControl?)
Bob Brady at 24-Sep-12 8:53am
Oh, my God, I'm stupid. Yes, the problem was that I left public off of the PropertiesViewModel. I can't believe that. That's what I get for working late on a Friday, huh?
Thanks, Matt.
As for the UserControl - that's the only way I know of to invoke it. I've been a WPF/MVVM programmer for about 3 weeks. Normally I do websites.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Peter Leow 295
1 Mika Wendelius 240
2 CHill60 195
3 TheRealSteveJudge 180
4 Zoltán Zörgő 178
0 Sergey Alexandrovich Kryukov 8,698
1 OriginalGriff 6,591
2 Peter Leow 3,982
3 Zoltán Zörgő 3,634
4 Richard MacCutchan 2,535

Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100