Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# 如何绑定用户控件属性?_C#_Winforms - Fatal编程技术网

C# 如何绑定用户控件属性?

C# 如何绑定用户控件属性?,c#,winforms,C#,Winforms,我有以下型号: public class Customer { public int Id {get; set;} public string Name {get; set;} public CategoryType Category {get; set;} } public class CategoryType { public int Id {get; set;} public string Name {get; set;} } 以及类别类型的用户

我有以下型号:

public class Customer
{
    public int Id {get; set;}
    public string Name {get; set;}
    public CategoryType Category {get; set;}
}

public class CategoryType
{
    public int Id {get; set;}
    public string Name {get; set;}
}
以及类别类型的用户控件:

[DefaultBindingProperty("Value")]
public partial class CategoryTypeControl : UserControl
{
    public class ValueChangedEventArgs
    {
        public T OldValue { get; private set; }
        public T NewValue { get; private set; }

        internal ValueChangedEventArgs(T oldValue, T newValue)
        {
            this.OldValue = oldValue;
            this.NewValue = newValue;
        }
    }
    public delegate void ValueChangedEventHandler(object sender, ValueChangedEventArgs e);
    public event ValueChangedEventHandler ValueChanged = delegate { };

    private CategoryType value;

    [Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
    public virtual CategoryType Value
    {
        get { return value; }
        set 
        {
            ValueChanged(this, new ValueChangedEventArgs(this.value, value));
            this.value = value; 
        }
    }
}
Customer
表单具有绑定到上述
Customer
模型和
Customer的bindingsource。CategoryType
绑定到
CategoryTypeControl.Value
。这是在设计模式下完成的,生成的代码是:

this.categoryType.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource, "CategoryType", true));
问题在于,当用户从控件中选择类别类型时,模型的
CategorType
属性没有更新


这不是绑定用户控件属性的正确方法吗?

如何绑定Customer的实例?发布该代码。@SriramSakthivel只需在表单上添加BindingSource控件,然后在设计模式下选择数据源。Customer.CategoryType如何绑定到CategoryTypeControl.Value?也许有问题?发布该代码将有所帮助。