5

我创建了自己的复选框(用户控件)。它有一个名为 ControlSource 的属性,用作绑定源。但是,ControlSource 的数据类型也是自定义类型。玩了一圈之后,

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

我的目标是我可以使用我自己的 bool 数据类型,它知道 true、false 或 null 的替代字符串表达式。但是,groupbox 控件一般应该进行编码。这就是我想到接口解决方案的原因。但是,它不起作用。我很确定有一个“经常使用”的解决方案,不是吗?

4

2 回答 2

0

与其扩展数据类型,不如实现一个 IValueConverter 来生成字符串表示?

http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

于 2013-01-03T05:10:26.150 回答
0

最后,我选择了一种解决方法:我继承了复选框并明确地将我的自定义数据类型用于控件源属性。因此,我不需要接口或抽象类......它不是最好的解决方案。

于 2013-01-02T12:01:03.313 回答