Verfin
Topic Author
Posts: 7
Joined: 07 Jun 2016, 11:46

C++ How to properly cast PropertyChangedEventArgs.newValue

14 Jun 2016, 09:59

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?
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

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

14 Jun 2016, 23:55

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());
}
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

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

15 Jun 2016, 17:58

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.
 
Verfin
Topic Author
Posts: 7
Joined: 07 Jun 2016, 11:46

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

17 Jun 2016, 09:34

Works now, thank you!

Who is online

Users browsing this forum: No registered users and 1 guest