Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:58:23+00:00 2026-06-16T04:58:23+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:58:24+00:00Added an answer on June 16, 2026 at 4:58 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple test application for the issue, two winforms each containing
I have a very simple application test in which I want to drag and
I have a simple test application where I want to have a clear button
I have written a simple test application using asp.net mvc with C#. The application
I have a C#-4 MVC3 RC test-application which is using Entity Framework 4. I
I have two POCOs in my Bookshelf test application: /// <summary> /// Represents a
I have a test application using the Lync 2010 SDK in full UI suppressed
I have a small test application that executes two threads simultaneously. One increments a
I am beginner in android. I have created one test application which consists of
I’m trying to set up an iPad test application, window-based, where I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.