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)); } } }