簡體   English   中英

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

[英]Text Box Text Changed event in WPF

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

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

我明白了。 當我更改它時,第二個文本框中的文本等於第一個文本框中的文本。 在此輸入圖像描述

但是當談到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>

您遇到空引用異常。 當創建textBox控件時,它將在textBox1上觸發textChange事件,到那時, 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}"

還有一個非常根本的原因是你需要清除並理解在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}" />

我希望這可以讓你和我自己理解。 這一切都取決於您定義自己的文本框行為......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM