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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:29:06+00:00 2026-05-23T08:29:06+00:00

I’m trying to get databinding set up in WPF. I’ve got the class person,

  • 0

I’m trying to get databinding set up in WPF. I’ve got the class person, which is updated (oldschool-like) through the one textbox, and the other textbox is supposed to mirror the change to the person object through a databinding (it used to be a type=twoway but that threw an xamlparseexception). It doesn’t work like that, and hitting the button that shows the person.name and it shows the correct name but the textbox doesn’t get updated via the databinding. Is this a bad way to try to understand databindings? If you’ve a better suggestion for a way to test it out I’m totally okay just ditching this code and doing that instead.

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:PeoplePleaser x:Key="PeoplePleaser" />
</Window.Resources>
<Grid>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <TextBox Height="125" HorizontalAlignment="Left" Margin="81,122,0,0" Name="textBox1" VerticalAlignment="Top" Width="388" FontSize="36" Text="{Binding Converter={StaticResource PeoplePleaser}, Mode=OneWay}" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="209,39,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" TextChanged="textBox2_TextChanged" />
</Grid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
            InitializeComponent();
    }

    public static Person myPerson = new Person();
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(myPerson.name);
    }

    private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
    {
       myPerson = new Person(textBox2.Text);
    }
}

public class Person
{
    public String name;

    public Person()
    {
        new Person("Blarg");
    }

    public Person(String args)
    {
        if (!args.Equals(null))
        {
            this.name = args;
        }
        else new Person();
    }

    public Person(String args, Person argTwo)
    {
        argTwo = new Person(args);
    }
}

public class PeoplePleaser : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            return MainWindow.myPerson.name;
        }
        catch (Exception e)
        {
            return "meh";
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!value.Equals(null))
        {
            return new Person(value.ToString(), MainWindow.myPerson);
        }

        else
        {
        return(new Person("", MainWindow.myPerson));
        }
    }
}
  • 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-23T08:29:07+00:00Added an answer on May 23, 2026 at 8:29 am

    There’s a bunch of problems here.

    The first and probably most significant one is that you’ve implemented Person.name as a field. Binding doesn’t work with fields. Person.name needs to be a property.

    The next issue that you have is that if you want a control to be updated with the value of a property when that property changes, your class has to implement property-change notification. (Which is another reason that Person.name has to be a property.)

    A third issue is that you’re using WinForms techniques in a WPF application. Data binding eliminates most of the use cases for the TextChanged event. (Not all: it can be useful when you’re developing custom controls.)

    A fourth issue is there’s no need for value conversion, so no need to implement a value converter.

    A Person class that implements property-changed notification correctly should look something like this:

    public class Person : INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;
    
       private void OnPropertyChanged(string propertyName)
       {
          PropertyChangedEventHandler h = PropertyChanged;
          if (h != null)
          {
             h(this, new PropertyChangedEventArgs(propertyName));
          }
       }
    
       public Person() { }
    
       public Person(string name)
       {
          Name = name;
       }
    
       private string _Name = "I was created by the parameterless constructor";
    
       public string Name
       { 
          get { return _Name; }
          set
          {
             if (_Name == value)
             {
                return;
             }
             _Name = value;
             OnPropertyChanged("Name");
          }
       }
    }
    

    Once you’ve done this, if you create a Person object and bind any TextBox objects’ Text properties to its Name property, they’ll all be maintained in sync, e.g.:

    <StackPanel>
       <StackPanel.DataContext>
          <local:Person Name="John Smith"/>
       </StackPanel.DataContext>
       <TextBox Text="{Binding Name, Mode=TwoWay}"/>
       <TextBox Text="{Binding Name, Mode=TwoWay}"/>
    </StackPanel>
    

    There’s much, much more to WPF data binding than this, but this should get you going down the right track.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I would like to run a str_replace or preg_replace which looks for certain words
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.