文本框文本在WPF中更改了事件

问题描述 投票:4回答:2

所以,例如,如果我在WFA中有2个文本框。以下代码有效。

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox2.Text = textBox1.Text;
    }

我明白了。当我更改它时,第二个文本框中的文本等于第一个文本框中的文本。 enter image description here

但是当谈到WPF时,我会得到完全不同的行为。当我这样做。

private void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        textBox1.Text = textBox.Text;
    }

然后按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>

c# wpf winforms textbox textchanged
2个回答
5
投票

您遇到空引用异常。当创建textBox控件时,它将触发textChange上的textBox1事件,到那时,textBox1不会被创建,因此为null。你可以改变XAML中文本框的顺序,你会没事的。但有一种更好的方法可以直接在XAML中使用Binding:

<TextBox x:Name="textBox" />
<TextBox x:Name="textBox1" Text="{Binding ElementName=textBox, Path=Text}" />

(我排除了一些属性以使示例更加干净)根据您希望更新其他文本框的时间,您可以将UpdateSourceTrigger添加到绑定:

Text="{Binding ElementName=textBox, Path=Text, UpdateSourceTrigger=PropertyChanged}"

1
投票

还有一个非常根本的原因是你需要清除并理解在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;
}

在你的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>

由ceciliaSHARP回答并由BDL编辑

在您的MainWindow.xaml.cs(代码隐藏)[WPF方式]

[Say no no and bye bye to the TextChangedEvent]

在您的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>

或第二个选项(WPF MVVM方式没有“发生即时更改事件”)

在MainWindow.xaml(View)中从上面的部分略有变化。

    <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}" />

添加新的模型代码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));
        }
    }
}

或使用UpdateSourceTrigger的第三个选项(WPF MVVM方式“发生即时更改事件”)

在MainWindow.xaml(View)中从上面的部分略有变化。

    <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}" />

我希望这可以让你和我自己理解。这一切都取决于您定义自己的文本框行为......

© www.soinside.com 2019 - 2024. All rights reserved.