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]
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
textBox1.Text = textBox.Text;
}
in your MainWindow.xaml (View) [Old Way]
<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>
As of answered by ceciliaSHARP & edited by BDL
in your MainWindow.xaml.cs (Code-Behind) [WPF Way]
[Say no no and bye bye to the TextChangedEvent]
in your MainWindow.xaml (View) [WPF Way]
<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" VerticalAlignment="Top" Width="120" />
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="212,124,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding ElementName=textBox, Path=Text}" />
</Grid>
</Window>
or the second option (WPF MVVM way with no "instantly changing event occurred")
in the MainWindow.xaml (View) slightly changed from the above part.
<TextBox HorizontalAlignment="Left" Height="23" Margin="212,77,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Path=Text, Mode=TwoWay}" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="212,124,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Path=Text, Mode=TwoWay}" />
add new piece of Model code SomeModelName.cs
using System.ComponentModel;
public class SomeModelName : INotifyPropertyChanged
{
private string text;
public string Text
{
set
{
if (text != value)
{
text = value;
RaisePropertyChanged("Text");
}
}
}
// some other properties and methods might go here
// ...
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
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.
<TextBox HorizontalAlignment="Left" Height="23" Margin="212,77,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBox HorizontalAlignment="Left" Height="23" Margin="212,124,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Path=Text, Mode=TwoWay}" />
I hoped this could make you and me myself understandable. It's all up-to-you to defined your own textbox behaviors...