user1910612
user1910612

Reputation: 43

How to get datacontext used in another xaml

I have a test application wich consists on two Windows and a UserControl.

I want to insert the control in each Window using the same DataContext:

MainWindow.xaml:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:view="clr-namespace:WpfApplication2.View"
    xmlns:viewModel="clr-namespace:WpfApplication2.ViewModel"
    Title="MainWindow" Height="350" Width="525">

<Window.DataContext>
    <viewModel:ControlColorViewModel x:Name="dataContext1"/>
</Window.DataContext>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0.2*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Button Grid.Column="0" Width="64" Height="64" Command="{Binding     
     Path=PressedButton}">Press</Button>

    <view:ControlColor Grid.Column="1" />

    </Grid>
</Window>

ControlColor.xaml:

<UserControl x:Class="WpfApplication2.View.ControlColor"
         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:viewModel="clr-namespace:WpfApplication2.ViewModel"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">


<Grid Background="{Binding Path=BackgroundColor}">

</Grid>
</UserControl>

ControlColorViewModel.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Input;

namespace WpfApplication2.ViewModel
{
class ControlColorViewModel : ViewModelBase
{
    private Brush backgroundColor;
    public Brush BackgroundColor
    {
        get { return this.backgroundColor; }
        set
        {
            if (this.backgroundColor != value)
            {
                this.backgroundColor = value;
                OnPropertyChanged("BackgroundColor");
            }
        }
    }

    public ICommand PressedButton { get { return new RelayCommand(param =>    
    this.SetPressedButton()); } }

    public ControlColorViewModel()
    {
    }

    private void SetPressedButton()
    {
        BackgroundColor = Brushes.Orange;
    }
  }
}

Window2.xaml:

<Window x:Class="WpfApplication2.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:view="clr-namespace:WpfApplication2.View"
    Title="Window2" Height="300" Width="300">
<Grid>

    <view:ControlColor />

</Grid>
</Window>

When the button is pressed the background in the ContentControl in the MainWindow gets orange, and i want the same for the ContentControl inserted in Window2. Using the same datacontext.

How can i get the same datacontext used in MainWindow?

Thanks in advance.

Upvotes: 1

Views: 775

Answers (3)

Lonli-Lokli
Lonli-Lokli

Reputation: 3766

There are a lot of ways. The easy is to have a common, shared model property (CommonModel) and send it to every ViewModels. Next step is to use EventAggregator, but to my mind it's too complicated for you..

Upvotes: 1

Brandon Moore
Brandon Moore

Reputation: 8780

I'm a little new to WPF (having more of a WinForms background), but if I understand correctly you could define your dataContext1 in the App file resources and then they both could reference it.

Upvotes: 0

Davio
Davio

Reputation: 4737

You can use something called Dependency Injection to get your controls to use the same DataContext.

Basically, you need to take it out of the XAML and 'find/resolve' your shared ControlColorViewModel in the constructor for your controls.

Upvotes: 0

Related Questions