下面是我的代码。
MainWindows.xaml :
<Window x:Class="WpfApp2.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:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Margin="5">
<TextBox x:Name="a" TextChanged="a_TextChanged" />
<TextBox x:Name="b" Margin="0 5 0 0" TextChanged="b_TextChanged" />
<TextBox x:Name="c" Margin="0 5 0 0" />
</StackPanel>
</Window>
MainWindow.xaml.cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void a_TextChanged(object sender,TextChangedEventArgs e)
{
b.Text = "bb";
}
private void b_TextChanged(object sender,TextChangedEventArgs e)
{
c.Text = "cc";
}
}
}
结果如下:
如您所见,当我手动更改a
中的文本时,b
被以编程方式更改了,从而又更改了c
。我希望b
的事件处理程序b_TextChanged
仅由用户操作触发(用户在b
中键入一些字符)。以编程方式更改b
的{{1}}属性不应触发Text
。我该如何实施?