'WPF MVVM - Views does not inherit the Data context from it's parent window

I ask this question after searching though a lot of solution and none of them worked,

My problem can be resumed this way: I have a main window (MainWindow.xaml), I have a view (ParametersView.xaml).

I also have the MainViewModel.cs file that would be my DataContext common to both view.

First I would like to make the parameter view inherit the DataContext from the MainWindows view, which is MainViewModel.

MainWindow.xaml

<Window x:Class="MVVMTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVMTest"
        mc:Ignorable="d"
        WindowStyle="None"
        AllowsTransparency="True"
        Background="Transparent"
        Title="MainWindow" Height="450" Width="800">

    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>

    <Border Background="#272537"
            CornerRadius="35">

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="70"/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <StackPanel Grid.Row="1">

                <RadioButton Content="Acquisition"
                             Height="50"
                             Foreground="White"
                             FontFamily="Century Gothic"
                             FontSize="20"
                             Style="{StaticResource MenuButtonTheme}"
                             />

                <RadioButton Content="Data visualization"
                             Height="50"
                             Foreground="White"
                             FontFamily="Century Gothic"
                             FontSize="20"
                             Style="{StaticResource MenuButtonTheme}"
                             Command="{Binding DataVisuViewCommand}"/>

                <RadioButton Content="Parameter"
                             Height="50"
                             Foreground="White"
                             FontFamily="Century Gothic"
                             FontSize="20"
                             Style="{StaticResource MenuButtonTheme}"
                             IsChecked="True"
                             Command="{Binding ParameterViewCommand}"/>

                <RadioButton Content="Post processing"
                             Height="50"
                             Foreground="White"
                             FontFamily="Century Gothic"
                             FontSize="20"
                             Style="{StaticResource MenuButtonTheme}"
                             Command="{Binding MembersViewCommand}"/>

                <Label x:Name="fqfs" Foreground="White"/>

            </StackPanel>

            <Button x:Name="Close_b" Grid.Column="1" Width="35" Height="35" HorizontalAlignment="Right" Margin="0,0,20,0" Style="{StaticResource PowerButton}" Click="close_a_Click"/>

            <Button x:Name="Minimize_b" Grid.Column="1" Width="35" Height="35" HorizontalAlignment="Right" Margin="0,0,75,0" Style="{StaticResource MinimizeButton}" Click="minimize_a_Click"/>

            <local:ParametersView Margin="10" Grid.Column="1" Grid.Row="1" x:Name="View" />
            <!--<ContentControl Grid.Column="1" Grid.Row="1" Margin="10" Content="{Binding CurrentView}"/>-->

        </Grid>

    </Border>
</Window>

Initially, I was using ContentControl, but since the problem come from the Context, I tested with a more straightforward way, and it still doesn't work.

ParametersView.xaml

<UserControl x:Class="MVVMTest.ParametersView"
             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" 
             xmlns:local="clr-namespace:MVVMTest"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800"
             DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Window}}">


    <StackPanel>
        <TextBlock Text="Parameter"
                   Foreground="White"
                   FontSize="30"
                   HorizontalAlignment="Left"
                   Margin="0,0,0,20"
                   FontFamily="Century Gothic"/>

        <StackPanel Orientation="Horizontal">

            <Button x:Name="Export" Content="Save" Width="250" Height="40" Background="#01202f" Foreground="White" Style="{StaticResource ParameterButton}"/>
            <Button x:Name="Import" Content="Load" Width="250" Height="40" Background="#01202f" Foreground="White" Style="{StaticResource ParameterButton}" Margin="20,0,0,0"/>
        
        </StackPanel>

        <Label x:Name="dataCOntextLabel" Foreground="White"/>
    </StackPanel>
</UserControl>

I tried to use the DataContext relative to the parent but it still doesn't work, I use a label (fqfs in MainWindow, and dataCOntextLabel in ParameterView) in order to view each dataContext, fqfs display what I want, but dataCOntext remain empty.

But since my view is defined as a children of the mainWindows, I should not even have to precise a DataContext in the Parameter view, yet it still doesn't work.

I precise that all the .cs file are still by default except for these two obvious line:

fqfs.Content = this.DataContext; (MainWindow.xaml.cs) dataCOntextLabel.Content = "DataContext = " + this.DataContext; (ParametersView.xaml.cs)

MainViewModel.cs is also empty, but since I never refer to any change of view, it shouldn't have any impact.

I been though most of the question on the subject on internet and searched by myself for a while but it seems I really can't figure what is wrong here, i would really appreciate it if someone have a solution to this problem.

EDIT 1: The reason I try this is because I need to understand how to properly set up a DataContext in a bigger project (where I also have this problem), the problem is that the project is a bit complex, so I choosed to find the solution in a new (and smaller) project before trying to implement it in the bigger project.

I need the MainWindow and the ParameterView to be able to access the same data (for instance a collection). So they both need the same dataContext.

Beside, if you have the answer for ContentControl as well (use in comment in MainWindows.xaml), it would be really helpful too as I meant to use that later for dynamic view.

Thank in advance.

ParameterView.xaml.cs

namespace MVVMTest
{
    /// <summary>
    /// Interaction logic for ParametersView.xaml
    /// </summary>
    public partial class ParametersView : UserControl
    {
        public ParametersView()
        {
            InitializeComponent();

            dataCOntextLabel.Content = "DataContext = " + this.DataContext;
        }
    }
}

MainWindow.xaml.cs

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            View.DataContext = this;

            fqfs.Content = this.DataContext;
        }


Solution 1:[1]

In MainWindow keep this:

    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>

Remove all other references to DataContext. For example, in MainWindow() constructor, remove everything except InitializeComponent(). Remove from the UserControl.DataContext in ParametersView.xaml.

Child controls always inherit the parent DataContext. What you must do is provide dot-prefixed paths to your relevant viewmodel properties.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NWoodsman