Click here to Skip to main content
Rate this: bad
good
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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:b="clr-namespace:LookupTermWizard.Behavior"
        xmlns:vm="clr-namespace:LookupTermWizard.ViewModel"
        Height="300" Width="500" removed="LightGray" DataContext="{Binding}" >
 
<Window x:Class="LookupTermWizard.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:LookupTermWizard.ViewModel"
        xmlns:v="clr-namespace:LookupTermWizard.View"
        xmlns:b="clr-namespace:LookupTermWizard.Behavior"
        Title="Lookup Term Wizard" Height="900" Width="1300">
    <Window.DataContext>
        <vm:MainWindowViewModel />
    </Window.DataContext>
        <StackPanel DockPanel.Dock="Left" VerticalAlignment="Top" Height="775" >
            <ContentControl>
                <v:Properties DataContext="{Binding PropertiesVM}" />
            </ContentControl>
 
And then instantiate the viewmodel for the user control from the MainWindowViewModel
 
        PropertiesViewModel propertiesVM;
 
        PropertiesViewModel PropertiesVM
        {
            get { return propertiesVM; }
            set
            {
                propertiesVM = value;
                OnPropertyChanged("PropertiesVM");
            }
        }
        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
Comments
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