我知道这很简单。
我有Mainwindow一个文本框。在文本框内容更改发生事件即textboxtext_changed之后,我希望文本框再次变为空。
我在其他类中有一个函数,它在textboxtext_changed. 我正在考虑仅在其他类中清除函数中的文本框,但我无法访问主窗口控件,我不想在那里创建主窗口实例。
有什么简单的方法吗?
public void function(ref TextBox textBox)
{
  textbox.Text = string.empty;
}
从您的 TextChanged 函数中,您可以从发件人访问 TextBox
private void textBox1_TextChanged(object sender, EventArgs e)
{
    ((TextBox)sender).Text = "";
}
使用MVVM可以非常简单:
TextBox.Text到此字符串属性并将 UpdateSourceTrigger 设置为 PropertyChanged 并将 Mode 设置为 TwoWay。视图模型
    public class MyViewModel : INotifyPropertyChanged
    {
        private string someText;
        public string SomeText
        {
            get
            {
                return this.someText;
            }
            set
            {
                this.someText = value;
                if (SomeCondition(this.someText))
                {
                    this.someText = string.Empty;
                }
                var epc = this.PropertyChanged;
                if (epc != null)
                {
                    epc(this, new PropertyChangedEventArgs("SomeText"));
                }
            }
        }
    }
XAML
    <TextBox Text="{Binding SomeText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>