Posted by: Zeeshan Amjad | September 16, 2009

Using VisualCollection in VB.Net

ContainerVisual is a concrete class is used as a container for other visual object. Here is a sample code to demonstrate this class.

  1: Imports System.Windows
  2: Imports System.Windows.Media
  3: Imports System.Windows.Input
  5: Public Class MyVisual
  6:     Inherits Visual
  8: End Class
 10: Public Class MyWindow
 11:     Inherits Window
 13:     Private mv As MyVisual
 14:     Private vc As VisualCollection
 16:     Public Sub New()
 17:         Title = "VisualCollection Class"
 18:         Width = 300
 19:         Height = 200
 21:         mv = New MyVisual()
 22:         vc = New VisualCollection(Me)
 24:         vc.Add(mv)
 26:         AddHandler MouseDown, AddressOf OnMouseDownHandler
 27:     End Sub
 29:     Public Shared Sub OnMouseDownHandler(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
 31:         Dim win As MyWindow
 32:         win = CType(sender, MyWindow)
 34:         Dim point As Point
 35:         point = New Point()
 36:         point.X = e.GetPosition(win).X
 37:         point.Y = e.GetPosition(win).Y
 39:         Dim screenPoint As Point
 40:         screenPoint =
 42:         Dim str As String
 43:         str = String.Format("Before Calling PointToScreen ({0}, {1})" & vbCrLf & _
 44:                             "After Calling PointToScreen ({2}, {3})", _
 45:                             point.X, point.Y, screenPoint.X, screenPoint.Y)
 47:         win.Content = str
 49:     End Sub
 50: End Class
 52: Public Class wpf15
 53:     Public Shared Sub Main()
 55:         Dim win As MyWindow
 56:         win = New MyWindow()
 58:         Dim app As Application
 59:         app = New Application()
 60:         app.Run(win)
 61:     End Sub
 62: End Class


The most important addition to ContainerVisual class is an addition of VisualCollection field. VisualCollection is one of very rare case in WPF that this class is not inherited by DependencyObject or DispatcherObject. This class directly inherits from the Object class. Here is a class diagram of this.


It is important to note that although this class is available only at the ContainerVisual and its subclasses, but Visual class still has two protected functions related to this named VisualChildernCount and GetVisualChild. VisualChildernCount always return 0 at Visual class level and GetVisualChild throws ArgumentOutOfRangeException. And it perfectly makes sense, because we don’t even have VisualCollection defined at this level. These functions are available only for its subclasses to override and provide proper implementation. It is one more reason to make Visual abstract class.

There is one more method to write something on the window and use Visual class functionality. Create an object of VisualCollection class and set Window class its parent at the time of it creation by passing its address in constructor. Then add the object of MyVisual class in the VisualCollection.

Here is the output of this program.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: