I am working on a program to allow users to draw objects on a screen. It needs to do this through code, not XAML. I have a class inheriting Canvas that implements protected functions like, AddVisualChild, AddLogicalChild, RemoveVisualChild, RemoveLogicalChild. These methods can only be accessed by an inheriting class - if I use the Canvas object instead of the subclass, these methods cannot be accessed. My class will draw lines, curves and other Visuals. But I want to also add a TextBox into which the user can type directly, which I haven't been able to do. If I create an instance of TextBox and add it the children of the my subclass, the textbox does not appear. I've verified that it's there in debug mode and using Snoop. Is there a property I need to set. Setting Visibility and ZIndex do nothing.
If i create a Canvas (not an instance of my DrawingCanvas class) in XAML and put a TextBox under it, it appears fine. But if I try to add a Visual in code to the Canvas using
it says: "cannot be converted to 'System.Windows.UIElement'. I apparently need to subclass Canvas (or Panel or UIElement) to override protected functions, like "AddVisualChild". It's a Catch 22: if I use a Canvas, I can't draw visuals, if I use my derived class, DrawingCanvas, added controls are invisible.
If I use an actual Canvas in XAML, I can add the textbox in code using
I am looking for either a solution that gets all the functionality of the protected methods of Canvas, or a solution that makes TextBox visible in my derived class.