views:

51

answers:

2

Hi Guys,

I get runtime error when I do this...

I have this class...

public abstract class AnnObject : DependencyObject

and when I do this it compiles fine, but throws a runtime error...

AnnObject aa;
var b = (DependencyObject)aa;
var c = (UIElement)b;

The error I get is cannot cast AnnObject to UIElement....

Can someone please briefly explain this behaviour?

Thanks,

Voodoo

+1  A: 

You only derive from DependencyObject , not from UIElement

Heinz K
+2  A: 

The class hiearchy in Silverlight for UI components is:-

DependencyObject
  UIElement
    FrameworkElement
      Control

So as Heinz points out you would need to have derived from UIElement order to be able to cast to UIElement and DependencyObject. Personnally I can't see deriving from DependencyObject being that useful. I would normally start at FrameworkElement, Control or even higher.

AnthonyWJones
Thanks AnthonyWJones, actually the AnnObject is a third party control class which I am using in my app.....I wonder if I can change it to UIElement. Basically I want it to be UIElement so I can use the ToolTipService on it, do you have any ideas how I can do this??? thanks again
VoodooChild
@VoodooChild: The only way I can think of would be for you to create an new UIElement based class that use the AnnObject as a component.
AnthonyWJones
@Anthony: Does it mean that I create a class eg InternalAnnObject which extends UIElement, and then uses AnnObject as one of its properties? If it is not much trouble, would you please update your answer with an example? thanks.
VoodooChild
@VoodooChild: From which third-party does this AnnObject come from? What does it do? How has it come to be that you want to make it a target of a tooltip whilst it doesn't appear to have a UI over which to hover a mouse (else it would have derived from UIElement) ?
AnthonyWJones
It is from "LEADTOOLS imaging SDK". It plots the annotation points on any image that I load into a container. I wanted the mouse over/tooltip features on these points. I asked this question on their forum and their reply was : "Your check using the HitTest method is correct. However, we don't have specific functions for displaying tooltips when hovering over an annotation object, so you should write the code yourself.You can try that by using the System.Windows.Forms.ToolTip class and keep changing the tooltip text using the ToolTip.SetToolTip method."
VoodooChild
They just posted this reply recently and at this point I am not sure what to do, but I will be working on it :)
VoodooChild
This is what I did: They had a hitTest method on the Container which contains these AnnObjects,which returns a collection. I am using this methods along with a timer which checks if the mouse point is on the same point for like a second. If HitTest returns a object and timer has elapsed for atleast a second then at this point I popup an alert with the information I want from this object...This seems to be working so far so good. Next step is to show some better control instead of an Alert/msg box.
VoodooChild
Could you have a look at this one and see if you know of something which might help me further http://stackoverflow.com/questions/3239100/showing-custom-tooltip-popup-when-hovering-over-an-object-in-silverlightThanks
VoodooChild