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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:09:35+00:00 2026-06-09T19:09:35+00:00

Datacontext does not work when I declare it in XAML. But the same works

  • 0

Datacontext does not work when I declare it in XAML. But the same works if set in Code.

Detailed Analysis.

My XAML

    <Window x:Class="SimpleDatabindingwithclass.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Name="windo">
    <Grid DataContext="{Binding ElementName=windo,Path=objectOfStudent}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" Margin="25" Height="25" Width="100" HorizontalAlignment="Left" Name="TextBox1" Text="{Binding Path=StudentName}"></TextBox>

</Grid>
</Window>

Corresponding code.

    namespace SimpleDatabindingwithclass
    {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
         public MainWindow()
         {
              InitializeComponent();
              Student objectOfStudent = new Student();
              objectOfStudent.StudentName = "John diley";
              objectOfStudent.Address = "20, North Travilia, Washington DC.";
              //not setting datacontext here since i set that in xaml
         }
         public class Student
         {
             private string studentname;
             private string address;

             public string Address
             {
                 get { return address; }
                 set { address = value; }
        }
        public string StudentName
        {
            get{return studentname;}
            set{studentname = value;}
        }
    }
}

}

But, the same when I use this XAML & set datacontext through code, it works!

ie, When I put something like

     this.DataContext = objectOfStudent; 

in MainWindow(), the application Works!
What do u think the problem is?

  • 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-09T19:09:37+00:00Added an answer on June 9, 2026 at 7:09 pm

    Binding only works with public properties, you can’t bind to some local variable. Make objectOfStudent as public property of your MainWindow.

    Edit:

    public Student objectOfStudent { get; set; }
    
    public MainWindow()
    {
        InitializeComponent();
        objectOfStudent = new Student();
        objectOfStudent.StudentName = "John diley";
        objectOfStudent.Address = "20, North Travilia, Washington DC.";
        //not setting datacontext here since i set that in xaml
     }
    

    Edit:

    Also you need to implement INotifyPropertyChanged interface in the MainWindow and Student classes and raise PropertyChanged when you set the properties. That is the right way, the binding will be updated everytime you change the properties. Or a simple way: create objectOfStudent before calling InitializeComponent.

    public MainWindow()
    {
        objectOfStudent = new Student();
        objectOfStudent.StudentName = "John diley";
        objectOfStudent.Address = "20, North Travilia, Washington DC.";
        InitializeComponent();
        //not setting datacontext here since i set that in xaml
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is a code that does not work. I made it work without validation,
When doing databinding does one have to implement INotifyPropertyChanged on the datacontext in WPF?
Duplicate of : Why are DataContext and ItemsSource not redundant? In WPF we can
The C# code is: DataContext db = new DataContext(MyProject.Properties.Settings.Default.MyString) I am trying to convert
I know this topic has been discussed, but not by me yet. As I
Would creating another datacontext in the GetData3() method lead to problems? public void SetoFDataMethods()
I have a custom DataContext with a Table member... Entry is defined as... [Table(Name=Entry)]
When using DataContext.CreateDatabase() to create a database, I wish to stop Linq To Sql
For example a = datacontext.customers.FirstOrDefaul(); b = datacontext.customers.Skip(1).FirstOrDefaul(); a.name=name1; b.Name=name2; When i call datacontext.SubmitChanges(),
I want the datacontext of my listview to be binded to an observable collection.

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.