Click here to Skip to main content
15,887,289 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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:

XML
<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

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


My Xaml for the UserControl

XML
<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

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


I hope someone can give me a piece of advice.

Thank you.
Posted
Comments
Irina Pykhova 8-May-13 9:37am    
set UserControl DataContext in the MainWindow after call to InitializeComponent()
pd1989 8-May-13 10:04am    
Thanks!
Sandeep Mewara 8-May-13 11:08am    
Is your problem resolved?

Yes, it's solved,

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

C#
public MainWindow()
   {
       this.DataContext = new MainViewModel();
       InitializeComponent();
       myUserControlName.DataContext = new UserControlViewModel();
   }
 
Share this answer
 
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,

C#
class MainViewModel : BaseViewModel
{
    public UserControlViewModel UserControlVM {get; set;} 

    public MainViewModel()
    {
        UserControlVM = new UserControlViewModel();
    }
}



and use XAML code in MainWindow like this,

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



Thanks.
 
Share this answer
 
Comments
pd1989 10-May-13 1:14am    
Thank you for your advice.
Regards
David Pizzari 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900