2012-12-29 3 views
5

나 자신의 확인란 (UserControl)을 만들었습니다. 바인딩 소스로 작동하는 ControlSource라는 속성이 있습니다. 그러나 ControlSource의 데이터 형식 또한 사용자 지정 형식입니다.C# Custom CheckBox + 사용자 지정 바인딩 데이터 형식

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

내 목표는 내가 참, 거짓 또는 널 (null)에 대한 대체 문자열 표현을 알고 내 자신의 부울 데이터 형식을 사용할 수 있다는 것입니다. 그러나 groupbox 컨트롤은 일반적으로 코딩해야합니다. 이것이 바로 인터페이스 솔루션이 내 마음 속에 들어온 이유입니다. 그러나 작동하지 않습니다. 나는 "자주 사용되는"해결책이 있다는 것을 확신합니다. 그렇지 않습니까?

답변

0

마지막으로 해결 방법을 선택했습니다. 확인란을 상속하고 컨트롤 원본 속성에 내 사용자 지정 데이터 형식을 명시 적으로 사용했습니다. 따라서 인터페이스 또는 추상 클래스가 필요하지 않습니다 ... 최상의 솔루션이 아닙니다.

관련 문제