Page 1 of 1

C++ How to properly cast PropertyChangedEventArgs.newValue

Posted: 14 Jun 2016, 09:59
by Verfin
As the OnPropertyChanged(DependencyPropertyChangedEventArgs& arg) arg.newValue is a const void pointer, and I don't know what type it is when the property is ForegroundProperty, I can't clone it and set it as a new foreground. How do I fix this:
if (e.prop == ForegroundProperty)
{
    //Brush* brush = (Brush*)((Brush*)arg.newValue)->Clone().GetPtr();
    ButtonControl->SetForeground((Brush*)arg.newValue);
}
Can the "GetType" sort of functions be leveraged in a future situations to find out what type the most derived class is, since the document is lacking in this front?

Re: C++ How to properly cast PropertyChangedEventArgs.newVal

Posted: 14 Jun 2016, 23:55
by sfernandez
Hi,

Foreground property is of type Brush, so you should be able to use its Clone method without problem:
if (e.prop == ForergroundProperty)
{
    Brush* brush = static_cast<const Ptr<Brush>*>(args.newValue)->GetPtr();
    Ptr<Brush> brushClone = NsStaticCast<Ptr<Brush> >(brush->Clone());
    ButtonControl->SetForeground(brushClone.GetPtr());
}

Re: C++ How to properly cast PropertyChangedEventArgs.newVal

Posted: 15 Jun 2016, 17:58
by jsantos
Both casts should work
Brush* brush = static_cast<const Ptr<Brush>*>(args.newValue)->GetPtr();
Brush* brush = static_cast<Brush*>(args.newValue);
Your problem was when doing the Clone(). As a new object is being created you need to store it in a Ptr<> to hold a strong reference.

The main reason we are using void* in newValue and oldValue is efficiency. We wanted to avoid boxing values in our first architecture. But right now, boxing is quite fast and we should probably reevaluate this decision.

Re: C++ How to properly cast PropertyChangedEventArgs.newVal

Posted: 17 Jun 2016, 09:34
by Verfin
Works now, thank you!