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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:06:05+00:00 2026-05-16T20:06:05+00:00

The nested ViewModel is set to the MainWindow DataContext: var mainWindow = new MainWindow();

  • 0

The nested ViewModel is set to the MainWindow DataContext:

var mainWindow = new MainWindow();
mainWindow.Show();
mainWindow.DataContext = new
{
    MyProperty = new
    {
        MySubProperty = "Hello"
    }
}

It is easy to bind to MySubProperty in XAML:

<Button Content="{Binding MyProperty.MySubProperty}"/>

How can I do this binding in code behind?

// MyButton.xaml.cs
public partial class MyButton : Button
{
    public MyButton()
    {
        InitializeComponent();
        // todo: add binding here
    }

    // I want this method called if this datacontext is set.
    // and called if MySubProperty changes and INotifyPropertyChange is implemented in the Datacontext.
    public void MySubPropertyChanged(string newValue)
    {
        // ...
    }
}

I have no access to MainWindow in MyButton.xaml.cs, so I cannot use it as a source.

The Button is just an example, but it would be a start.
In my original scenario I have no useful dependency property for that. If a dp is necessary for such a binding, an example would be very helpful that includes the creation of a dp.

  • 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-16T20:06:06+00:00Added an answer on May 16, 2026 at 8:06 pm

    How about this? (just a dirty example and untested, should work in principle)

    // MyButton.xaml.cs
    public partial class MyButton : Button
    {
        public MyButton()
        {
            InitializeComponent();
            this.DataContextChanged += DataContext_Changed;
        }
    
        private void DataContext_Changed(Object sender,DependencyPropertyChangedEventArgs e)
        {
           INotifyPropertyChanged notify = e.NewValue as INotifyPropertyChanged;
           if(null != notify)
           {
              notify.PropertyChanged += DataContext_PropertyChanged;
           }
        }
    
        private void DataContext_PropertyChanged(Object sender,PropertyChangedEventArgs e)   
        {
            if(e.PropertyName == "MySubProperty")
               MySubPropertyChanged((sender as YourClass).MySubProperty);
        } 
    
        public void MySubPropertyChanged(string newValue)
        {
            // ...
        }
    }
    

    EDIT:

    for binding something in codebehind you can use:

    Binding binding = new Binding();
    // directly to myproperty
    binding.Source = MyProperty;
    binding.Path = new PropertyPath("MySubProperty");
    // or window
    binding.Source = mainWindow; // instance
    binding.Path = new PropertyPath("MyProperty.MySubProperty");
    
    // then wire it up with (button is your MyButton instance)
    button.SetBinding(MyButton.MyStorageProperty, binding);
    //or
    BindingOperations.SetBinding(button, MyButton.MyStorageProperty, binding);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some nested view models that implement INotifyPropertyChanged . I'd like to bind
I've got a nested dict like: Public collectionMain As New Dictionary(Of String, Dictionary(Of String,
i have a nested listview, i can bind the selected item of the basic
I have the issue that i have a viewmodel (MasterViewModel) with a nested viewmodel
I have some nested tables that I want to hide/show upon a click on
I believe we cannot bind to internal viewmodel. Therefore I think all binding related
I have a ViewModel class composed of several nested classes: public class UserAccountViewModel :
I have the following nested viewmodel class... public class CustomerModel { public string name;
As the title says, I want to bind a property from my ViewModel to
I'll keep it short. My view model looks like this: var ViewModel = {

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.