.net – 为什么当我在父对象上引发通知事件时,为什么Windows Forms数据绑定需要设置我的嵌套布尔数据绑定属性?

前端之家收集整理的这篇文章主要介绍了.net – 为什么当我在父对象上引发通知事件时,为什么Windows Forms数据绑定需要设置我的嵌套布尔数据绑定属性?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,所以这里是我的问题的一些上下文,写为伪C#代码(随意指出任何错误):(你可以直接跳转到stacktrace并稍后阅读上下文)
public class SomeForm {
    private _model = new viewmodelClass
    public void new() {
        // Normal Winforms init omitted
        viewmodelClassBindingSource.DataSource = _model;
        SomeControl1.SetModel(_model);
    }
}
public class SomeControl {
    private _model = new viewmodelClass

    internal void SetModel(viewmodelClass model) {
        _model = model;
        viewmodelClassBindingSource.DataSource = model;
        viewmodelClassBindingSource.ResetBindings(true);
    }
}

public class ComplexObject : IPropertyChanging,IPropertyChanged {
    public property bool BoolProp {get; set;}
}

public class viewmodelClass : IPropertyChanged {
    property IList<ComplexObject> ComplexObjects {get;}

    property ComplexObject SelectedComplexObject {get; set;}

    property Object SomethingNotNecessarilyRelated {get; set;}

    private void NotifyPropertyChanged(string propName) {
        PropertyChanged(this,new PropertyChangedEventArgs(propName));
    }
}

这些类中的所有属性都在Visual Studio 2008 Windows窗体设计器,SomeForm或SomeControl类中进行数据绑定. (ComplexObject.BoolProp在两者中都是数据绑定的).不要犹豫,就更多的问题询问上下文.

问题:当我在viewmodelClass类中进行一些(通知)通知时,使用这种堆栈跟踪,可以将ComplexObject.BoolProp设置为false:

System.dll!System.ComponentModel.ReflectPropertyDescriptor.SetValue(object component = "Object Exposed in 'SelectedComplexObject'",object value = false) + 0x124 bytes 
System.Windows.Forms.dll!System.Windows.Forms.BindToObject.SetValue(object value) + 0x5d bytes  
System.Windows.Forms.dll!System.Windows.Forms.Binding.PullData(bool reformat,bool force) + 0x15a bytes 
System.Windows.Forms.dll!System.Windows.Forms.BindingManagerBase.PullData(out bool success = true) + 0x6e bytes 
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.ParentCurrencyManager_CurrentItemChanged(object sender = {System.Windows.Forms.CurrencyManager},System.EventArgs e) + 0x54 bytes   
[Native to Managed Transition]  
[Managed to Native Transition]  
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.OnCurrentItemChanged(System.EventArgs e) + 0x17 bytes 
System.Windows.Forms.dll!System.Windows.Forms.CurrencyManager.List_ListChanged(object sender,System.ComponentModel.ListChangedEventArgs e) + 0x3bc bytes   
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x7e bytes    
System.Windows.Forms.dll!System.Windows.Forms.BindingSource.InnerList_ListChanged(object sender,System.ComponentModel.ListChangedEventArgs e) + 0x2e bytes 
System.dll!System.ComponentModel.BindingList<System.__Canon>.OnListChanged(System.ComponentModel.ListChangedEventArgs e) + 0x17 bytes   
System.dll!System.ComponentModel.BindingList<MyCompany.viewmodelClass>.Child_PropertyChanged(object sender,System.ComponentModel.PropertyChangedEventArgs e) + 0x176 bytes 
[Native to Managed Transition]  
[Managed to Native Transition]  
MyCompany.Gui.exe!MyCompany.viewmodelClass.NotifyPropertyChanged(String propertyName = "SomethingNotNecessarilyRelated") Line 437 + 0x3c bytes  Basic

为什么程序想要将SomeBool设置为false?我怎么能防止呢?

@H_404_11@
@H_404_11@
我在Stack Overflow中的第一个问题是Windows窗体应用程序中的一个字段,其中包含一个意想不到的值,就像你的.解决方案是等待窗体加载事件被触发以设置窗体的GUI元素.

我将推迟设置_model(包括使用新建构造)和其他GUI元素,直到在窗体加载事件的处理程序中.

如何:

在Visual Studio添加表单加载处理程序:

>在图形视图中打开窗体(例如,在解决方案资源管理器中双击SomeForm.cs)
>双击表单,外部任何控件或其他GUI元素(例如,在标题栏中).这将添加一个名为SomeForm_Load的函数的框架代码,这行是this.Load = new System.EventHandler(this.SomeForm_Load);将添加SomeForm.Designer.cs.

将设置代码移动到SomeForm_Load:

private void SomeForm_Load(object aSender,EventArgs anEvent)
{
    _model = new viewmodelClass;

    viewmodelClassBindingSource.DataSource = _model;
    SomeControl1.SetModel(_model);
}

从_model的声明中删除“= new viewmodelClass”.

@H_404_11@ 原文链接:https://www.f2er.com/windows/363945.html

猜你在找的Windows相关文章