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

  • Home
  • SEARCH
  • 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 931089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:26:41+00:00 2026-05-15T20:26:41+00:00

Greetings folks! I’m running into a problem with WPF databinding that I hope you

  • 0

Greetings folks!
I’m running into a problem with WPF databinding that I hope you can help out with. I’m new to WPF but an expereienced developer (VB 3.0-6.0, C#).

Here’s the scenario:
I have a C# project called MasterPartsData which contains a number of classes which reprsent different types of parts (capacitor, diode, etc). They inherit from a base class called clsPart.

I have another C# WPF project which contains WPF UserControls (as well as a MainWindow) to visually represent the values stored in an individual MasterPartsData (MPD) object. I’ve created a private field in the usercontrol to hold the object with a getter and setter.

If I create a binding explicitly in the setter for the populated object:

_capacitor = value;
Binding binding = new Binding();
binding.Source = _capacitor;
binding.Path = new PropertyPath("C0uf");
this.txtC0uf.SetBinding(TextBox.TextProperty, binding);

(with _capacitor being the private object variable and C0uf being the property name)
the value correctly displays.

However I don’t wish to have to explicitly create each binding in the code behind. My preference is to create the bindings inline in XAML, perhaps with a DataContext pointing to the object.

Unfortunately every different permutation I’ve tried fails to work; the text box doesn’t show data.

I have a couple of suspicions:
1) The binding is correct, but the text box needs to be refreshed.
2) The binding is confused between the private variable and the properties.
3) Maybe the fact that the class is defined in a different project is causing issues.
4) I’m going mad and should check myself into an asylum before someone gets hurt. 🙂

Any help you can provide would be most appreciated. I’m more than happy to add more information, but didn’t want to clutter the question with pages and pages of source.

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

    With respect to your suspicions:

    1) I think the default binding behavior of a TextBox is TwoWay, with a LostFocus update trigger, meaning that your UI focus will have to change to another control before the binding will update, if changes are made in the UI.

    If changes are made in the code you need to raise the NotifyPropertyChanged event in order for the binding system to see it.

    2) This is probably not the case, but it leaves the impression that you’re trying to set bindings on your UserControl properties, which is not the way data binding was designed to be used in this particular kind of use case. What you want is to bind data from non-UI classes to dependency properties on your UserControls.

    3) This will never matter, as long as your UI project has a reference to your classes.

    4) This is a common reaction people have when beginning to use XAML and WPF. It’s like instead of being handed a box of Legos, you just got handed an injection molding machine with insufficient instructions, isn’t it?

    Overall, this is a situation where you might need to examine your design; elements of the “Model-View-ViewModel” pattern will come in handy. If you’re unfamiliar with this, it’s a development pattern in which you introduce a “ViewModel” class, perhaps you can call it MasterPartsVM which contains an implementation of INotifyPropertyChanged.

    The DataContext of your UserControl would be set to this MasterPartsVM class.

    A brief code example, using some generic names. Given a ViewModel class with a small backing class that looks like this:

    class PartViewModel : INotifyPropertyChanged
    {
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        #endregion
    
        public PartClass Data { get; set; }
    
        public String SomeVMProperty
        {
            get { return Data.SomeProperty; }
    
            set
            {
                if (Data.SomeProperty != value)
                    Data.SomeProperty = value;
                this.PropertyChanged(this, new PropertyChangedEventArgs("SomeVMProperty"));
            }
        }
    
    }
    
    class PartClass
    {
        public string SomeProperty { get; set; }
    }
    

    The XAML of a basic UserControl would look like this:

    <UserControl x:Class="WpfApplication1.PartUserControl"
                ... >
        <Grid>
            <TextBox Text="{Binding SomeVMProperty}" Margin="68,77,104,176" />
        </Grid>
    </UserControl>
    

    To connect your data class to this UserControl, you set the UserControl’s DataContext property. If you do this in code, it’s a matter of having a reference to your user control and the ViewModel, and then setting the property:

    MyUserControlInstance.DataContext = new PartViewModel(); // or some existing PartViewModel
    

    That combination of code should work to produce a textbox whose Text property changes every time the SomeVMProperty property is changed.

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

Sidebar

Related Questions

Greetings! I know that UITableView sectionHeaderHeight is only used for grouped tables, but I'll
Greetings, creating my first MVVM based WPF app and trying to figure out why
Greetings folks. I have this weird problem. In the project I'm working on now,
Greetings! I have an issue here that i can't find. I am getting a
Greetings, I have a particular object which can be constructed from a file, as
Greetings P4 folks, I am trying to understand the P4 describe -ds output. I
Greetings, using VS2008 and VisualSVN and seems the VisualSVN folks are religious about updating
Folks, I've got an ASP.NET MVC application that I am attempting to secure using
Greetings, In an WPF DataGridTemplateColumn I have a CellTemplate using a ListView and a
Greetings Everyone I am new in asp.net and i'm using RadControls for Asp.net Ajax

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.