AddVisualChild question

I have a design where I want to load a visual dynamically from an assembly (a plug-in system). I won't know which assembly or namespace ahead of time, so this has to happein in code-behind and not in xaml.

I have created a class that derives from FrameworkElement called PluginHost. PluginHost loads the visual from the assembly and creates it just fine, but I can't figure out how to get the visual instance added to the PluginHost. AddVisualChild doesn't seem to do anything. VisualChildrenCount still shows zero and nothing appears after the call.

Can someone help



Answer this question

AddVisualChild question

  • Aali

    Thank you for your excellent clarification. I hope we can see this level of detail in the documentation at some point. That's exactly what I needed to know.
  • smartravi

    Thanks, Jared -- I am following up with our documentation team to improve the documentation around this topic, as I'm sure it will be a common question for control implementers.

  • Jayanthi Prasad

    If you are inheriting from a class that has already overriden the Visual protected methods then you should generally not override or call those directly anymore because you'll break the encapsulation of that base class. In the case of DockPanel, for example, you should use the Children property exposed by Panel. That's the only way that panels can reliably deal with Visual children.



  • Dmitri Sologoub

    To add visuals to your class you need to override the protected virtual methods in the Visual class. My apologies for not having the documentation handy, but I believe they are VisualChildrenCount (yes, your class doesn't query this, it implements it) and GetVisualChild. You then have to maintain the child collection.

    The AddVisualChild method is a notification to the base class that you've acquired a new child into your collection. It doesn't actually add the child to any collection, but it lets the Visual system know that it needs to redraw. This sounds a bit funny, but it's consistent with the way the logical tree is managed as well.

    Similarly, when you remove a Visual from your child collection you must call RemoveVisualChild, so the Visual system knows to stop tracking and displaying that Visual. If you'd like, and it sounds appropriate in your scenario, you don't have to use an actual collection for your child visuals. You can simply have a member variable of type Visual, which you can think of as a collection of maximum length 1. Your implementation of GetVisualChild should return the value of that variable, and your implementation of VisualChildrenCount should probably return 0 if that variable is null, or 1 if it's non-null.



  • David Evan

    Alright, I understand that if I derive from Visual I must handle my own child collection, etc. But what if I make a class that derives from DockPanel

    It seems that when I add a child element in XAML to a DockPanel it shows just fine. But if I try to use AddVisualChild in C# code, nothing appears. If I try to use AddChild in C# code nothing appears and if I try both I get an exception that the element already has a parent.

    Am I missing something obvious How do I add a child element to a DockPanel in code and get it to appear on the screen

    Thanks,

    Jared


  • AddVisualChild question