2012-12-29 42 views
5

Tôi đã tạo hộp kiểm của riêng mình (UserControl). Nó có một thuộc tính được gọi là ControlSource hoạt động như một nguồn ràng buộc. Tuy nhiên, Kiểu dữ liệu của ControlSource cũng là một kiểu tùy chỉnh. Sau khi chơi đùa,C# Custom CheckBox + Kiểu dữ liệu ràng buộc tùy chỉnh

... 
    [System.ComponentModel.Bindable(true), Browsable(true), Category("Behavior")] 
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
    public IMyBool ControlSource 
    { 
     get { return this.chx.Checked ? IMyBool.True : IMyBool.False; } 
     set { this.chx.Checked = value.Value; } 
    } 
    ... 

Tôi đã thử một giải pháp giao diện ở đây:

public interface IMyBool 
{ 
    bool Value { get; set; } 

    IMyBool True { get; } 
    IMyBool False { get; } 
} 

public class MyBool 
{ 
    protected bool? _bValue = null; 

    protected string _sTrue = String.Empty; 
    protected string _sFalse = String.Empty; 
    protected string _sNull = String.Empty; 

    public MyBool(bool? bValue = null) 
    { 
     this._bValue = bValue; 
    } 

    public override string ToString() 
    { 
     return this._bValue.HasValue ? (this._bValue.Value ? _sTrue : _sFalse) : _sNull; 
    } 

    public bool Value 
    { 
     get { return this._bValue.Value; } 
     set { this._bValue = value; } 
    } 
} 

public class MyInvoiceBool : MyBool, IMyBool 
{ 
    public static implicit operator MyInvoiceBool(bool? bValue) 
    { 
     return bValue.HasValue ? (bValue.Value ? True : False) : Null; 
    } 
    public static implicit operator bool?(MyInvoiceBool ivbValue) 
    { 
     return ivbValue._bValue; 
    } 

    public MyInvoiceBool(bool? bValue = null) 
    { 
     base._sTrue = "Rechnung wird gestellt"; 
     base._sFalse = "Rechnung wird nicht gestellt"; 
     base._bValue = bValue; 
    } 

    public static MyInvoiceBool True 
    { 
     get { return new MyInvoiceBool(true); } 
    } 
    public static MyInvoiceBool False 
    { 
     get { return new MyInvoiceBool(false); } 
    } 
    public static MyInvoiceBool Null 
    { 
     get { return new MyInvoiceBool(); } 
    } 
} 

public class MyInvoiceAddressBool : MyBool 
{ 
    public static implicit operator MyInvoiceAddressBool(bool? bValue) 
    { 
     return bValue.HasValue ? (bValue.Value ? True : False) : Null; 
    } 
    public static implicit operator bool?(MyInvoiceAddressBool ivbValue) 
    { 
     return ivbValue._bValue; 
    } 

    public MyInvoiceAddressBool(bool? bValue = null) 
    { 
     base._sTrue = "Abweichende Rechnungsadresse"; 
     base._bValue = bValue; 
    } 

    public static MyInvoiceAddressBool True 
    { 
     get { return new MyInvoiceAddressBool(true); } 
    } 
    public static MyInvoiceAddressBool False 
    { 
     get { return new MyInvoiceAddressBool(false); } 
    } 
    public static MyInvoiceAddressBool Null 
    { 
     get { return new MyInvoiceAddressBool(); } 
    } 
} 

Mục đích của tôi là tôi có thể sử dụng riêng kiểu dữ liệu bool của tôi mà biết một biểu thức chuỗi thay thế cho đúng, sai hoặc null. Tuy nhiên, điều khiển hộp nhóm phải được mã hóa nói chung. Đó là lý do tại sao một giải pháp giao diện xuất hiện trong đầu tôi. Tuy nhiên, nó không hoạt động. Tôi khá chắc chắn rằng có một giải pháp "thường được sử dụng", phải không?

Trả lời

0

Cuối cùng, tôi đã chọn một công việc xung quanh: Tôi đã kế thừa hộp kiểm và sử dụng rõ ràng loại dữ liệu tùy chỉnh của tôi cho thuộc tính nguồn điều khiển. Vì vậy, tôi không cần một giao diện hoặc lớp trừu tượng ... Nó không phải là giải pháp tốt nhất.

Các vấn đề liên quan