Reputation: 750
[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);
}
}
Now when I change the Amount, the Buyer Bro is not updating automatically. How to achieve it? Let me know, if i have to provide some additional info
Upvotes: 3
Views: 5410
Reputation: 65
Did you try adding,
[RefreshProperties(RefreshProperties.Repaint)]
to the 2 properties inside your Brokerage class?
Upvotes: -1
Reputation: 1307
Adding the attribute "[NotifyParentProperty(true)]" will do the trick:
[ReadOnly(false)]
[NotifyParentProperty(true)]
public Decimal Amount
{
get { return _Amt; }
set { _Amt = value; }
}
Make sure to add
using System.ComponentModel;
Now when you change Amount and loose the focus of this distinct property, the parent property will automatically be updated. Cheers!
Upvotes: 4
Reputation: 18031
I had the same issue in the past.
I did put [RefreshProperties(RefreshProperties.Repaint)]
or RefreshProperties.All
, then I implemented INotifyPropertyChanged
on my target objects, but I never managed to get the automatic mechanism it to work correctly.
I ended using the .Refresh()
method on the PropertyGrid. Now it works all the time.
var b = new Brokerage(10, "EUR");
this.propertyGrid1.SelectedObject = b;
...
b.Amount = 20;
this.propertyGrid1.Refresh();
Upvotes: 3