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 6472435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:20:40+00:00 2026-05-25T06:20:40+00:00

As a continuation of the question Linking DataContext with another property in WPF .

  • 0

As a continuation of the question Linking DataContext with another property in WPF.

At the very end of the research I was very surprised to find out that when one writes something like that:

<Label Content="{Binding Path=Name}" />

The DataContext against which the Content property is binded is of the Label control itself! The fact that it still works is due to the default inheritance of the DataContext value from the nearest parent.

But if you have this label wrapped in a custom control, and you don’t want to bind your data to the DataContext property of that control, you would more likely love to have:

<Controls:SearchSettings Settings="{Binding Path=Settings}" />

And here you are. Now you need to set Settings as the DataContext for the SearchSettings control, for Label inside to bind against, but you can’t, because that will trigger re-binding of Settings property.

I can’t see the point in mixing binding properties using different sources: DataContext, by ElementName, etc.
So why would I ever use DataContext?

  • 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-25T06:20:40+00:00Added an answer on May 25, 2026 at 6:20 am

    When you write

    <Label name="myLabel" Content="{Binding Path=Name}" />
    

    you are binding to myLabel.DataContext.Name, and not myLabel.Name.

    The XAML in WPF is just a pretty user interface to display and interact with the actual data, otherwise known as the DataContext. The purpose of other binding sources (RelativeSource, ElementName, etc) is to point to another property that doesn’t exist in the current control’s DataContext


    So suppose you have a Window. Without setting the DataContext, the window still displays but there is no data behind it.

    Now suppose to set myWindow.DataContext = new ClassA();. Now the data that the window is displaying is ClassA. If ClassA has a property called Name, I could write a label and bind it to Name (such as your example), and whatever value is stored in ClassA.Name would get displayed.

    Now, suppose ClassA has a property of ClassB and both classes have a property called Name. Here is a block of XAML which illustrates the purpose of the DataContext, and an example of how a control would refer to a property not in it’s own DataContext

    <Window x:Name="myWindow"> <!-- DataContext is set to ClassA -->
        <StackPanel> <!-- DataContext is set to ClassA -->
    
            <!-- DataContext is set to ClassA, so will display ClassA.Name -->
            <Label Content="{Binding Name}" />
    
             <!-- DataContext is still ClassA, however we are setting it to ClassA.ClassB -->
            <StackPanel DataContext="{Binding ClassB}">
    
                <!-- DataContext is set to ClassB, so will display ClassB.Name -->
                <Label Content="{Binding Name}" />
    
                <!-- DataContext is still ClassB, but we are binding to the Window's DataContext.Name which is ClassA.Name -->
                <Label Content="{Binding ElementName=myWindow, Path=DataContext.Name}" /> 
            </StackPanel>
        </StackPanel>
    </Window>
    

    As you can see, the DataContext is based on whatever data is behind the UI object.

    Update: I see this question so often from new WPF users that I expanded this answer into a post on my blog: What is this “DataContext” you speak of?

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

Sidebar

Related Questions

Design problem- one function calls all three I continuation from another question after that
This question is in continuation of this one . The deal is simple. Given:
This question is a continuation of my last one, regarding How to make Ruby
This is a continuation of my previous question: Could not find an implementation of
This question is a continuation of this one but asking for a more specific
This question is an continuation on another StackOverflow post where no clear answer is
A continuation from this question I need a SQL statement that returns the number
This is a continuation of my last question . In that question I showed
This is a continuation of another question I have. Consider the following code: char
This is a continuation question from a previous question I have asked I now

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.