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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:53:29+00:00 2026-06-08T16:53:29+00:00

In a nutshell, I want to use DataContext binding to assign a label the

  • 0

In a nutshell, I want to use DataContext binding to assign a label the value of a public automatic property in another class.

So I have a class containing a public automatic property like so:

public class MyData
{
    public string DogName { get; set; }
}

My WPF form looks like this:

WPF

The CodeBehind for my WPF form is as so:

public partial class MainWindow : Window
{
    private MyData myData;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        myData = new MyData();
        myData.DogName = "Lulu";

        label1.DataContext = myData.DogName;
    }
}

This isn’t, however, changing the value of label1 to “Lulu”, it just stays at “Label”. What have I missed out?

Thanks

  • 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-08T16:53:30+00:00Added an answer on June 8, 2026 at 4:53 pm

    That is not exactly the common way to work with DataContexts (as you show it, MyData as a class serves no purpose at all).
    Try this instead:

    //C#
    label1.DataContext = new MyData{ DogName = "Lulu" };
    
    //xaml
    <Label Content="{Binding Path=DogName}"/>
    

    now, you could for instance also use MyData to contain the label’s Width or so.

    Another (probably most used) way is to set the parent’s DataContext, and make the individual elements use it’s properties:

    //C#
    class MyData
    {
      public strig DogName{ get; set; }
      public strig CatName{ get; set; }
    }
    
    this.DataContext = new MyData{ DogName = "Lulu", CatName = "Fifi" };
    
    //xaml
    <Label Content="{Binding Path=DogName}"/>
    <Label Content="{Binding Path=CatName}"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a nutshell , I want to have different faces for some types of
In a nutshell: Using jquery I want to get some xml output and use
In a nutshell I have two images I want to overlay one over the
In a nutshell, I have a solution that builds fine in the IDE, and
In a nutshell I want a callback to fire after the html has been
I have a dilemma going on here. I need to use a session to
I want to group the results of a count(*) query into value buckets. I'm
I think I have got some grasp over basic WPF fundaes. Now I want
In a nutshell, I want my JQuery UI Slider to look like this: Is
In a nutshell, there's a global stylesheet: a { font-family: Arial; } I want

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.