Rom*_*man 5 c# wpf textbox winforms textchanged
所以,例如,如果我在WFA中有2个文本框.以下代码有效.
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
Run Code Online (Sandbox Code Playgroud)
我明白了.当我更改它时,第二个文本框中的文本等于第一个文本框中的文本.

但是当谈到WPF时,我会得到完全不同的行为.当我这样做.
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
textBox1.Text = textBox.Text;
}
Run Code Online (Sandbox Code Playgroud)
然后按Ctrl + F5测试应用程序,没有任何反应.日志显示"Build Succeeded"并没有.这有什么不对?
这是XAML代码.
<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>
Run Code Online (Sandbox Code Playgroud)
小智 8
您遇到空引用异常.当textBox创建控制时,会触发textChange事件textBox1,并通过这一点,textBox1没有创建,因此无效.你可以改变XAML中文本框的顺序,你会没事的.但有一种更好的方法可以直接在XAML中使用Binding:
<TextBox x:Name="textBox" />
<TextBox x:Name="textBox1" Text="{Binding ElementName=textBox, Path=Text}" />
Run Code Online (Sandbox Code Playgroud)
(我排除了一些属性以使示例更加干净)根据您希望更新其他文本框的时间,您可以将UpdateSourceTrigger添加到绑定:
Text="{Binding ElementName=textBox, Path=Text, UpdateSourceTrigger=PropertyChanged}"
Run Code Online (Sandbox Code Playgroud)
还有一个非常根本的原因是,您需要清除并了解在WPF中,最终目标实际上是更少的,使用View进行Code-Behind编码会更好,实际做法是我们都使用Binding方式倾向于更简洁的方式,并满足关键的可维护性,可测试性和可扩展性。我们不喜欢像常规应用程序实现模式那样的老式方法。而您反对这个概念。
在您的MainWindow.xaml.cs(隐藏代码)中[旧方法]
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox2.Text = textBox1.Text;
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
textBox1.Text = textBox.Text;
}
Run Code Online (Sandbox Code Playgroud)
在您的MainWindow.xaml中(查看)[旧方法]
<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>
Run Code Online (Sandbox Code Playgroud)
截至ceciliaSHARP答复,并由BDL编辑
在您的MainWindow.xaml.cs中(代码隐藏)[WPF方式]
[Say no no and bye bye to the TextChangedEvent]
Run Code Online (Sandbox Code Playgroud)
在您的MainWindow.xaml(视图)中[WPF方式]
<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>
Run Code Online (Sandbox Code Playgroud)
或第二个选项(WPF MVVM方式,不发生“即时更改事件”)
在MainWindow.xaml(视图)中,与上述部分略有不同。
<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}" />
Run Code Online (Sandbox Code Playgroud)
添加新的模型代码片段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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
或第三个选项(使用WPF MVVM方式,“发生了即时更改的事件”),使用UpdateSourceTrigger
在MainWindow.xaml(视图)中,与上述部分略有不同。
<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}" />
Run Code Online (Sandbox Code Playgroud)
我希望这可以使您和我自己都能理解。定义自己的文本框行为完全由您决定...
| 归档时间: |
|
| 查看次数: |
23369 次 |
| 最近记录: |