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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:40:04+00:00 2026-05-15T09:40:04+00:00

I wish to define the DataContext of my window from an external class to

  • 0

I wish to define the DataContext of my window from an external class to be able to use DI for my data model.
I have read some tutorials about it but I still can’t get it to work together. Say we have a simple data model :

class Data 
{
    public String Value { get; set; }

    public Data() 
    {
        Value = "Test";
    }
}

When I instanciate the data object into the XAML code, the data binding operates correctly :

<Window ...>
  <Window.Resources>
    <src:Data x:Key="data" />
  </Window.Resources>
  <Window.DataContext>
    <Binding Source="{StaticResource ResourceKey=data}" />
  </Window.DataContext>
  <Grid>
    <Label Content="{Binding Path=Value}" />
  </Grid>
</Window>

But if I try to bind the data from an external class, the window just shows nothing and I get no error:

<Window ...>
  <Grid>
    <Label Content="{Binding Path=Value}" />
  </Grid>
</Window>

And the main class :

class Test
{
    [@STAThreadAttribute()]
    public static void Main(string[] args)
    {
        MainWindow w = new MainWindow();

        Binding b = new Binding();
        b.Source = new Data();
        w.DataContext = b;

        w.ShowDialog();
    }
}

Am I missing something ? Maybe the DataContext property must be setted from a different thread ?

  • 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-15T09:40:04+00:00Added an answer on May 15, 2026 at 9:40 am

    You can set the Data directly in your code behind:

    class Test
    {
        [@STAThreadAttribute()]
        public static void Main(string[] args)
        {
            MainWindow w = new MainWindow();
    
            w.DataContext = new Data();
    
            w.ShowDialog();
        }
    }
    

    Or use the Binding, and you should set the Binding differently in code behind:

    class Test
    {
        [@STAThreadAttribute()]
        public static void Main(string[] args)
        {
            MainWindow w = new MainWindow();
    
            Binding b = new Binding();
            b.Source = new Data();
            SetBinding(DataContextProperty, b);
    
            w.ShowDialog();
        }
    }
    

    in your example you set the Binding as DataContext, meaning you will bind not to the Data, but to the Binding object itself. If you use Xaml, it will intenally determine if you use a binding, and use the latter instead of the first!

    Hope this helps!

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

Sidebar

Related Questions

I have written a class in ruby and I wish to define the method
I have a class that already has a 'natural' order, and wish to define
I wish to define the following typeclass Mapping : {-# LANGUAGE MultiParamTypeClasses #-} class
I wish HTML could do something semantically equivalent to this; <dl class=main-list> <definitionitem> <dt>Some
I wish to be able to change the table a class is mapped to
I am using Matplotlib and Numpy to produce some plots. I wish to define
I have a clj file using a certain namespace and I wish to define
Suppose I have a variables in the global scope. Suppose I wish to define
In python, I defined a class 'number'. Now I wish to define a function
I have a map in C++ and I wish to input my class as

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.