wpfuser-controlsdrawingcontext

WPF Canvas and DrawingVisuals disappear


I have a set of User Controls, derived from FrameworkElement. Each host one or more DrawingVisuals. These drawing visuals can represent text, graphics, progress meters etc and are used to display the states of various HMI devices.

These, appearance-wise work fine in a grid - images are rendered, text and backgrounds appear fine, dependency properties can be set. However, when I encapsulate them in a Canvas, they disappear all but for the drawing visuals representing labels. And even for these, only the text rendered by DrawingContext.DrawText is visible. Operations performed by dc.DrawImage, cd.DrawRectangle etc do not appear.

Can someone please shed some light on why this might be.

Thanks


Solution

  • Are you positioning your controls properly? In a grid you have margins for each item, where as in a canvas you have to set Canvas.Top and Canvas.Left for each element. Is it not that those items are hidden underneath something? Try using snoop to use if the controls are actually part of Canvas children or if they have not been added at all.

    You can read more about Canvas on MSDN