在WPF应用程序中,如何使事件仅响应用户操作?

下面是我的代码。

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

结果如下:

在WPF应用程序中,如何使事件仅响应用户操作?

如您所见,当我手动更改a中的文本时,b被以编程方式更改了,从而又更改了c。我希望b的事件处理程序b_TextChanged仅由用户操作触发(用户在b中键入一些字符)。以编程方式更改b的{​​{1}}属性不应触发Text。我该如何实施?

mydisable 回答:在WPF应用程序中,如何使事件仅响应用户操作?

也许是时候切换到MVVM了?

private string _a;
public string A
{
    get { return _a; }
    set { SetA(value,true); }
}

protected void SetA(string value,bool isUserInput)
{
    _a = value;
    if (isUserInput)
    {
        // aditional operations
    }
    OnPropertyChanged("A");
}

TextBox中的用户输入将通过绑定触发属性设置器,isUserInput将为true

<TextBox x:Name="a" Text="{Binding A}" />

可以使用带有false标志:SetA("new value",false);的Set方法对程序逻辑进行任何更改

,

简单的使用标志来声明,更改背后的代码正在运行:

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool _nonUserChange = false;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void a_TextChanged(object sender,TextChangedEventArgs e)
        {
            DoNonUserChange(() => b.Text = "bb");
        }

        private void b_TextChanged(object sender,TextChangedEventArgs e)
        {
            DoNonUserChange(() => c.Text = "cc");
        }

        private void DoNonUserChange(Action act)
        {
            if (act != null && !_nonUserChange)
            {
                _nonUserChange = true;
                act();
                _nonUserChange = false;
            }
        }
    }
}
本文链接:https://www.f2er.com/3161689.html

大家都在问