So, for example if I have 2 text boxes in WFA. The following code works.
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
And I get this. The text in the second text box equals to the text in the first one, when I change it.
But when it comes to WPF, I get a completely different behavior. When I do this.
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
textBox1.Text = textBox.Text;
}
And press Ctrl+F5 to test the application, nothing happens. The log says "Build Succeeded" and nothing. What is wrong here?
And here is the XAML code.
<Window x:Class="TextBoxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TextBoxTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="212,77,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="textBox_TextChanged"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="212,124,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
You are encountering a null reference exception. When the
textBox
control is created it will trigger thetextChange
event ontextBox1
and by that point,textBox1
isn't created and is therefore null. You can just change the order of the textboxes in the XAML and you will be fine. But there is a nicer way of doing this, directly in XAML with Binding:(I excluded some attributes to make the example more clean) Depending on WHEN you want the other textbox to update you can add UpdateSourceTrigger to the binding:
And one more very fundamental reason is that you need to be cleared and understanding that in WPF practically ultimate goal is less Code-Behind coding with the View will be better, the actual practice is we all used the Binding way tend to do it in a much more cleaner manner, and fulfill the keys Maintainability, Testability, and Extensibility. We don't like the old-fashioned way of like the normal application implemented pattern. And you against this concept.
in your MainWindow.xaml.cs (Code-Behind) [Old Way]
in your MainWindow.xaml (View) [Old Way]
As of answered by ceciliaSHARP & edited by BDL
in your MainWindow.xaml.cs (Code-Behind) [WPF Way]
in your MainWindow.xaml (View) [WPF Way]
or the second option (WPF MVVM way with no "instantly changing event occurred")
in the MainWindow.xaml (View) slightly changed from the above part.
add new piece of Model code SomeModelName.cs
or the third option (WPF MVVM way with "instantly changing event occurred"), using the UpdateSourceTrigger
in the MainWindow.xaml (View) slightly changed from the above part.
I hoped this could make you and me myself understandable. It's all up-to-you to defined your own textbox behaviors...