C# 在用户控件之间移动字符串不起作用

C# 在用户控件之间移动字符串不起作用,c#,winforms,C#,Winforms,我试图在表单之间传递字符串。为什么不呢?我是遗漏了什么,还是程序中有错误 关于UserControl3 UserControl1 u1; public UserControl3() { u1 = new UserControl1(); InitializeComponent(); } public void materialCheckBox1_CheckedChanged(object sender, EventArgs

我试图在表单之间传递字符串。为什么不呢?我是遗漏了什么,还是程序中有错误

关于UserControl3

 UserControl1 u1;

    public UserControl3()
    {
           u1 = new UserControl1();
           InitializeComponent();
    }
public void materialCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (materialCheckBox1.Checked)
            {
                u1.toUserControl3 = "GOINTHEBOX!";
            }
            else
            {
                u1.toUserControl3 = string.Empty;
            }


        }
关于UserControl3

 UserControl1 u1;

    public UserControl3()
    {
           u1 = new UserControl1();
           InitializeComponent();
    }
public void materialCheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (materialCheckBox1.Checked)
            {
                u1.toUserControl3 = "GOINTHEBOX!";
            }
            else
            {
                u1.toUserControl3 = string.Empty;
            }


        }
关于UserControl1

public string toUserControl3
        {
            get
            {
                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }
public void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
关于UserControl1

public string toUserControl3
        {
            get
            {
                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }
public void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

通过一段代码更改控件上的
Text
属性并不一定意味着值控件将更新。通常,您需要在属性(在本例中为
ToUserControl 3
)和控件之间进行某种绑定。您需要一种方法来告诉控件该值已更改,以便它知道如何更新

您可以通过以下方式完成数据绑定:

创建一个新类来处理状态和绑定:这样就不需要将控件传递给其他控件的构造函数

public class ViewModel : INotifyPropertyChanged
{
    public string TextBoxText => CheckBoxChecked ? "GOINTOTHEBOX!" : string.Empty;

    public bool CheckBoxChecked
    {
        get { return _checkBoxChecked; }
        set 
        { 
            _checkBoxChecked = value; 
            OnPropertyChanged("CheckBoxChecked"); 
        }
    }

    private bool _checkBoxChecked;
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
这是您的主要表单

public void Form1
{
    public Form1(ViewModel viewModel)
    {
        UserControl1.DataBindings.Add("TextBoxTextProperty", viewModel, "TextBoxText");
        UserControl3.DataBindings.Add("MaterialCheckBoxCheckedProperty", viewModel, "CheckBoxChecked");
    }
}
UserControl1

public void UserControl1()
{
    public string TextBoxTextProperty
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }
}
UserControl3

public void UserControl3()
{
    public bool MaterialCheckBoxCheckedProperty
    {
        get { return materialCheckBox1.Checked; }
        set { materialCheckBox1.Checked = value; }
    }
}

刚刚意识到可能有更好的方法,因为您处理的是用户控件而不是表单,并且不希望将对视图模型的依赖性注入到用户控件中