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

  • SEARCH
  • Home
  • 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 7057477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:59:27+00:00 2026-05-28T03:59:27+00:00

Disclaimer I feel like this is a fairly simple question, so i must reiterate

  • 0

Disclaimer I feel like this is a fairly simple question, so i must reiterate that I did look for an answer and couldn’t find anything!

Not sure if I am asking the question correctly, but I will tell you this. I am working on becoming more familiar with MVVM, so I am messing around with a ridiculously simple project of two stackpanels, ten textboxes, and some simple binding. Everything works now, since I have two panels, which separates my boxes and lets me set two datacontext.

My question is this:

a) is it possible to set the datacontext on a parent element (Stackpanel) and have half of my child elements (textboxes) use that context via inheritance and then give the other half of the elements A DIFFERENT data context?

and

b) if this is possible, how??

Thanks people-smarter-than-I

Here is the code that is trying so hard, but not really doing anything I want it to be doing:

XAML

   <Grid>
    <StackPanel>
    <StackPanel HorizontalAlignment="Left" Margin="8,8,0,75" Width="224" DataContext="{Binding Path=Customer}">
        <TextBox Text="{Binding Path=FirstName}"  Height="28" Name="label1"/>
        <TextBox Text="{Binding Path=MiddleName}"  Height="28" Name="l2"/>
        <TextBox Text="{Binding Path=LastName}"  Height="28" Name="l3"/>
        <TextBox Text="{Binding Path=CompanyName}"  Height="28" Name="l4"/>
        <TextBox Text="{Binding Path=EmailAddress}"  Height="28" Name="l5"/>

        <!--I want the next five TextBox elements to bind to a different datacontext-->

        <TextBox Text="{Binding Path=FirstName}"  Height="28" Name="label11"/>
        <TextBox Text="{Binding Path=MiddleName}"  Height="28" Name="l21"/>
        <TextBox   Text="{Binding Path=LastName}"  Height="28" Name="l1lgh3"/>
        <TextBox Text="{Binding Path=CompanyName}"  Height="28" Name="l1hj4"/>
        <TextBox Text="{Binding Path=EmailAddress}" Height="28"/>
   </StackPanel>
   </StackPanel>
</Grid>

Code Behind C#

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

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = new MainViewModel();
    }
}

View Model

   public class MainViewModel : INotifyPropertyChanged
{
    public MainViewModel()
    {
        PopulateCustomerInfo();
    }

    private Customer customer;
    public Customer Customer
    {
        get { return customer; }
        set
        {
            customer = value;
            OnPropertyChanged("Customer");
        }
    }

    private Customer customer2;
    public Customer Customer2
    {
        get { return customer2; }
        set
        {
            customer2 = value;
            OnPropertyChanged("Customer");
        }
    }


    private void PopulateCustomerInfo()
    {
        AdventureWorksLTE ctx = new AdventureWorksLTE();
        this.Customer = (from c in ctx.Customers
                   select c).FirstOrDefault();
        this.Customer2 = (from c in ctx.Customers
                          orderby c.FirstName descending
                          select c).FirstOrDefault();

    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handle = PropertyChanged;
        if (handle != null)
        {
            var e = new PropertyChangedEventArgs(propertyName);
            handle(this, e);
        }
    }

}
  • 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-05-28T03:59:28+00:00Added an answer on May 28, 2026 at 3:59 am

    Well, you can do various things like changing the DataContext locally on all those lower TextBoxes:

    <TextBox Text="{Binding Path=FirstName}" DataContext="..."/>
    

    The question really is though: What do you want to achieve? Does it make sense to even set the DataContext on the StackPanel?

    Maybe you should not and use a longer path instead:

    <TextBox Text="{Binding Path=Customer.FirstName}"  Height="28" Name="label1"/>
    

    It all depends on what properties are going to be used in the child controls, if most or all of them are in Customer, sure, set it to keep the bindings short. If you need the main view model in some places you can use RelativeSource to get to a control with the right data context and change the path accordingly. (DataContext.*, data context appears in the path as a different source is specified)

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

Sidebar

Related Questions

Disclaimer: This is not actually a programming question, but I feel the audience on
Disclaimer While this question looked like a potential duplicate , it was resolved by
Disclaimer: I'm very new to C. I tried to find an answer to this
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial
Disclaimer: I'm a rails n00b. I'm playing around with a simple helper function that
Disclaimer: this question is driven by my personal curiosity more than an actual need
Background / Disclaimer First of all, please feel free to skip this entirely if
Disclaimer: This question is for understanding. I'll use boost::lexical_cast in the field. It has
I have a Website which has a disclaimer which says that this web site
Disclaimer: I tried to search for similar question, however this returned about every C++

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.