Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么从绑定中删除BindingManagerBase_C#_Winforms_Binding - Fatal编程技术网

C# 为什么从绑定中删除BindingManagerBase

C# 为什么从绑定中删除BindingManagerBase,c#,winforms,binding,C#,Winforms,Binding,我有一个很奇怪的案子。。。。 我们有一个带有文本框的用户控件,它绑定到绑定源 BindingSource bindingSource = new BindingSource(); this.bindingSource.Add(this.viewMode); this.textBox.DataBindings.Add(new Binding("EditValue", "Name", this.bindingSource, OnPropertyChanged)); 我们在运行时创建此用户控件的实

我有一个很奇怪的案子。。。。 我们有一个带有文本框的用户控件,它绑定到绑定源

BindingSource bindingSource = new BindingSource();
this.bindingSource.Add(this.viewMode);

this.textBox.DataBindings.Add(new Binding("EditValue", "Name", this.bindingSource, OnPropertyChanged));
我们在运行时创建此用户控件的实例,并将其添加到列表中,例如

MyUserControl view = new MyUserControl();
ourViews.Add(view);
如果我们在此视图上导航,它将添加到表单中:

MyUserControl view = (MyUserControl)ourViews(0);
this.Controls.Add(view);
在这一点上(我们第一次这样做),绑定工作正常,值在两个方向上都得到更新

之后,将导航其他一些视图,并从表单中删除该视图:

this.Controls.Clear();
OtherUserControlView view = (OtherUserControl)ourViews(1);
this.Controls.Add(view);
有时,我们的旧视图会再次被导航: MyUserControl视图=(MyUserControl)我们的视图(0); this.Controls.Add(视图)

现在有一个问题。绑定不再更新。我们认识到,绑定实例的BindingManagerBase为null(自从创建绑定以来,我们没有接触它),并且绑定的IsBinding属性设置为false。那么这是什么原因呢?我们如何才能再次激活绑定

谢谢你的帮助,
Eny

启用LayoutControl.UseLocalBindingContext后,绑定对我来说很好。 此属性是隐藏的,无法在设计器中设置。您可以在代码中设置它


我在develxpress支持中心找到了此解决方案。

我正在做类似的事情,并找到了不同的解决方法。添加用户控件时,将其BindingContext备份到某个位置(我有一个“StackedControl”结构)。删除它时,请将其还原。这似乎解决了绑定问题。

谢谢,您的解决方案甚至比每次手动设置绑定上下文更好。我已经第二次被这个错误咬到了,因为我忘记了我花了5个小时才明白这一点。欺骗我两次。。。