Click here to Skip to main content
11,649,476 members (75,472 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
Hi,

I have a User Control in a Window. I need a DataContext for the Window and another one for the UserControl. The problem is that the DataContext from the Window inherits to the DataContext from the User Control. That means, after initializing the application I lost my DataContext from the UserControl, but have the DataContext from the Window at both, Window and UserControl.

When I don't set the DataContext of the Window then my User Control gets the right one. That means, each DataContext allocation works for it's own, but not together.

My Xaml for the window:

<Window x:Class="myproj.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:my="clr-namespace:myproj.Controls"
        xmlns:myBehaviour="clr-namespace:Behaviour"
        Title="window" WindowState="Maximized" myBehaviour:Behaviour.Command="{Binding Command}">
 
        <Grid>
 <Grid.ColumnDefinitions>            
            <ColumnDefinition x:Name="TreeViewColumn" Width="250" />
            <ColumnDefinition x:Name="MainColumn" MinWidth="200" />
 </Grid.ColumnDefinition>
 <Grid.RowDefinitions>
            <RowDefinition x:Name="MenuBarRow" Height="56" />            
            <RowDefinition x:Name="MainRow" MinHeight="200" Height="200*" />
  </Grid.RowDefinition>
 
        <my:UserControl Grid.Column="0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        </Grid>
        </Window>

My Code - behind constructor for the window

public MainWindow()
   {
       this.DataContext = new MainViewModel();
       InitializeComponent();
   }

My Xaml for the UserControl

<UserControl x:Class="myproj.Controls.myUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300" >
 
    <DockPanel>
        <TextBox  TextWrapping="Wrap" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Text="{Binding myProperty, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Top" />
    </DockPanel>
</UserControl>

My Code - behind constructor for the User Control

public UserControl()
   {
      InitializeComponent();
      this.DataContext = new UserControlViewModel;
   }

I hope someone can give me a piece of advice.

Thank you.
Posted 8-May-13 2:49am
pd1989715
Comments
Irina Pykhova at 8-May-13 9:37am
   
set UserControl DataContext in the MainWindow after call to InitializeComponent()
pd1989 at 8-May-13 10:04am
   
Thanks!
Sandeep Mewara at 8-May-13 11:08am
   
Is your problem resolved?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Yes, it's solved,

now my constructor of my mainwindow looks like this...

public MainWindow()
   {
       this.DataContext = new MainViewModel();
       InitializeComponent();
       myUserControlName.DataContext = new UserControlViewModel();
   }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hello pd1989,

You are right but,
By the way its not a good way to use datacontext properties. Better way to initiate UserControlViewModel in MainViewModel's constructor. Look This,

class MainViewModel : BaseViewModel
{
    public UserControlViewModel UserControlVM {get; set;} 
 
    public MainViewModel()
    {
        UserControlVM = new UserControlViewModel();
    }
}


and use XAML code in MainWindow like this,

<my:usercontrol grid.column="0" grid.row="1" horizontalalignment="Stretch" verticalalignment="Stretch" datacontext="{Binding" xmlns:my="#unknown"></my:usercontrol>


Thanks.
  Permalink  
Comments
pd1989 at 10-May-13 1:14am
   
Thank you for your advice.
Regards
David Pizzari at 7-Oct-14 23:27pm
   
Doesn't this run through the constructor of the UserControl twice? Once for the xaml version and once where you have initiated it in code? I ask because I'm having a similar problem which led me here

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

  Print Answers RSS
0 F-ES Sitecore 354
1 OriginalGriff 324
2 Kornfeld Eliyahu Peter 309
3 CPallini 295
4 Sergey Alexandrovich Kryukov 254
0 OriginalGriff 1,577
1 Sergey Alexandrovich Kryukov 1,087
2 CPallini 1,015
3 DamithSL 996
4 jyo.net 994


Advertise | Privacy | Mobile
Web03 | 2.8.150804.4 | Last Updated 9 May 2013
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