我有以下型号:
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.CategoryType
绑定到CategoryTypeControl.Value
。这是在设计模式下完成的,生成的代码是:
this.categoryType.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource, "CategoryType", true));
问题是,当用户从控件中选择类别类型时,模型的CategorType
属性不会更新。
这不是绑定用户控件属性的正确方法吗?