Share via


StylusPointCollection

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents a collection of related StylusPoint objects.

<StylusPointCollection   ...>
  oneOrMoreStylusPoints
</StylusPointCollection>

XAML Values

Value

Description

oneOrMoreStylusPoints

One or more StylusPoint object elements.

Managed Equivalent

StylusPointCollection

Remarks

This collection keeps track of the StylusPoint objects that define the appearance of the Stroke object.

A StylusPointCollection can belong to more than one Stroke object; however, the only way to do this is by assigning an existing StylusPoints value of one Stroke to another in script.

Collection methods such as Add or GetItem will expect or return objects that are of type StylusPoint. In addition to defining Collection methods, this collection also defines the method AddStylusPoints.

The XAML syntax for properties that use a StylusPoint is potentially an example of implicit collection syntax, in which you can omit an actual StylusPointCollection object element. However, prepopulating an InkPresenter with strokes is not a common technique; usually you start with a blank Strokes collection and capture strokes as user input with runtime JavaScript.

Example

The following example code shows a basic ink capture and presentation technique, including an Erase mode.

// Define variables.
var agCtrl;
var inkPresenter; // Corresponds to the InkPresenter element in XAML.
var newStroke = null; // The Stroke variable for mouse handlers.

function root_Loaded(sender, args)
{
  // Get the HTML object that contains the Silverlight plug-in.
  agCtrl = sender.GetHost();

  // Hold on to the InkPresenter plug-in interface.
  inkPresenter = sender.findname("inkPresenterElement");
}

// Capture mouse movement and create the stroke.
function InkPresenterMouseDown(sender,args)
{
  inkPresenter.CaptureMouse();

  // Inverted stylus means enter Erase mode
  if (args.GetStylusInfo().IsInverted)
  {
    var sc = agCtrl.content.createFromXaml("<StrokeCollection/>");
    sc = inkPresenter.Strokes.HitTest(args.GetStylusPoints(inkPresenter));
    
    for (var i = 0; i < sc.Count; i++)
    {
      inkPresenter.Strokes.Remove(sc.GetItem(i));
    }
  }
  else // Ink mode
  {
    newStroke = agCtrl.content.createFromXaml('<Stroke/>');

    var da = agCtrl.content.createFromXaml('<DrawingAttributes/>');
    newStroke.DrawingAttributes = da;
    var spc = agCtrl.content.createFromXaml('<StylusPointCollection/>');
    newStroke.StylusPoints = spc;

    newStroke.DrawingAttributes.Width = 5;
    newStroke.DrawingAttributes.Height = 5;
    newStroke.DrawingAttributes.Color = "Green";
    newStroke.DrawingAttributes.OutlineColor = "Black";

    newStroke.StylusPoints.AddStylusPoints(args.GetStylusPoints(inkPresenter));
    inkPresenter.Strokes.Add(newStroke);
  }
}

// Add the new points to the Stroke 
// or delete strokes if you are in erase mode.
function inkPresenterMouseMove(sender, args)
{
    var stylusPoints = args.getStylusPoints(sender);
   
    // Erase Mode
    if (lastErasePoint != null)
    {
        // Connect the point from previous mouse event
        // to the current collection of stylus points.
        stylusPoints.insert(0, lastErasePoint);
        var hitStrokes = sender.strokes.hitTest(stylusPoints);

        // Remove the strokes that were intersected above.
        for (var i = 0; i < hitStrokes.Count; i++)
        {
          sender.strokes.remove(hitStrokes.getItem(i));
        }
        
        // Update the cached last erase point.
        lastErasePoint = stylusPoints.getItem(stylusPoints.count-1);
    }

    // Ink Mode
    if (newStroke != null)
    {
        newStroke.stylusPoints.addStylusPoints(stylusPoints);
    }
}


// Release the mouse.
function InkPresenterMouseUp(sender,args)
{
  newStroke = null;
  inkPresenter.ReleaseMouseCapture();
}

function ClearInkMouseDown(sender,args)
{
  inkPresenter.Strokes.Clear();
}