3

在此处输入图像描述

[TypeConverter(typeof(BrokerageConverter))]
[DescriptionAttribute("Brokerage Details")]
[PropertyGridInitialExpanded(true)]
[RefreshProperties(RefreshProperties.Repaint)]
public class Brokerage
{
    private Decimal _Amt = Decimal.Zero; private string _currency = "";

    public Brokerage() { }
    public Brokerage(Decimal broAmount, string broCurrency) { Amount = broAmount; Currency = broCurrency; }

    [ReadOnly(false)]
    public Decimal Amount 
    {
        get { return _Amt; }
        set { _Amt = value; }
    }

    [ReadOnly(true)]
    public string Currency
    {
        get { return _currency; }
        set { _currency = value; }
    }

    //public override string ToString() { return _Amt.ToString() + " - " + _currency; }
}

public class BrokerageConverter : ExpandableObjectConverter
{

    public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
    {
        if (destinationType == typeof(Brokerage))
            return true;

        return base.CanConvertTo(context, destinationType);
    }


    public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
    {
        if (t == typeof(string))
        {
            return true;
        }
        return base.CanConvertFrom(context, t);
    }

    // Overrides the ConvertFrom method of TypeConverter.
    public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture, object value)
    {
        if (value is string)
        {
            string[] v = ((string)value).Split(new char[] { '-' });
            return new Brokerage(Decimal.Parse(v[0]), v[1]);
        }
        return base.ConvertFrom(context, culture, value);
    }
    // Overrides the ConvertTo method of TypeConverter.
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(System.String) && value is Brokerage)
        {
            Brokerage b = (Brokerage)value;
            return b.Amount.ToString() + " - " + b.Currency.ToString();
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

现在,当我更改金额时,Buyer Bro 不会自动更新。如何实现?让我知道,如果我必须提供一些额外的信息

4

3 回答 3

4

添加属性“[NotifyParentProperty(true)]”就可以了:

[ReadOnly(false)]
[NotifyParentProperty(true)]
public Decimal Amount 
{
    get { return _Amt; }
    set { _Amt = value; }
}

确保添加

using System.ComponentModel;

现在,当您更改 Amount 并失去此独特属性的焦点时,父属性将自动更新。干杯!

于 2015-06-18T08:11:32.563 回答
3

我过去也有同样的问题。

我确实 put [RefreshProperties(RefreshProperties.Repaint)]or RefreshProperties.All,然后我INotifyPropertyChanged在我的目标对象上实现了,但我从来没有设法让它的自动机制正常工作。

显然,我并不 孤单

我结束了使用.Refresh()PropertyGrid 上的方法。现在它一直有效。

var b = new Brokerage(10, "EUR");
this.propertyGrid1.SelectedObject = b;

...

b.Amount = 20;
this.propertyGrid1.Refresh();
于 2013-03-06T13:42:03.680 回答
-1

您是否尝试添加,

[RefreshProperties(RefreshProperties.Repaint)]

到您的经纪类中的 2 个属性?

于 2014-10-26T17:23:17.417 回答