0

私はそれが単純なものであることを知っています。

私はMainwindowテキストボックスを持っています。テキストボックスの内容を変更すると、イベントが発生textboxtext_changedします。その後、テキストボックスを再び空にしたいです。

私は他のクラスに関数を持っていて、それはで実行されtextboxtext_changedます。他のクラスの機能でテキストボックスをクリアすることを考えていますが、メインウィンドウコントロールにアクセスできず、そこにメインウィンドウのインスタンスを作成したくありません。

それを行う簡単な方法はありますか?

4

3 に答える 3

2
public void function(ref TextBox textBox)
{
  textbox.Text = string.empty;
}
于 2013-01-24T11:33:24.547 に答える
1

TextChanged 関数から、送信者から TextBox にアクセスできます

private void textBox1_TextChanged(object sender, EventArgs e)
{
    ((TextBox)sender).Text = "";
}
于 2013-01-24T11:34:03.927 に答える
0

MVVMを使用すると、非常に簡単になります。

  1. ViewModel で文字列プロパティを宣言します。
  2. プロパティをこの文字列プロパティにバインドし、TextBox.TextUpdateSourceTrigger を PropertyChanged に、モードを TwoWay に設定します。
  3. ViewModel でプロパティが変更されるたびにロジックを実行します。

ビューモデル

    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}"/>
于 2013-01-24T12:04:03.767 に答える