2012-12-29 167 views
5

我創建了自己的複選框(UserControl)。它有一個名爲ControlSource的屬性,它用作綁定源。但是,ControlSource的數據類型也是一種自定義類型。玩耍,C#自定義複選框+自定義綁定數據類型

... 
    [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; } 
    } 
    ... 

後,我在這裏嘗試的界面解決方案:

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(); } 
    } 
} 

我的目標是,我可以用哪知道真正的,虛假或無效的替代字符串表達式我自己的布爾數據類型。但是,組合框控件應該一般編碼。這就是爲什麼一個界面解決方案出現在我腦海裏。但是,它不起作用。我確信有一個「經常使用」的解決方案,不是嗎?

回答

0

最後,我選擇了一項解決方法:我繼承了複選框,並明確將自定義數據類型用於控件源屬性。因此,我不需要接口或抽象類...它不是最好的解決方案。