2

I would like to add some text (like a label) on a InkCanvas with WPF in C# code (not Xaml).

How is it possible ? thanks Jonathan

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
user96547
  • 583
  • 10
  • 18

4 Answers4

2

You can do something like this. (See MSDN docs for SetTop, SetLeft)

// add control to children collection 
// (ctlToAdd can be TextBlock, RichTextBox, FlowDocument for example
inkCanvas.Children.Add(ctlToAdd);
InkCanvas.SetTop(ctlToAdd, 100.0);
InkCanvas.SetLeft(ctlToAdd,100.0);
Mark K
  • 121
  • 3
1

Try to implement a custom Stroke class and use Drawingcontext.DrawString method for the same.

biju
  • 17,554
  • 10
  • 59
  • 95
1
private void drawTextBox(object sender, MouseEventArgs e)  
{  
TextBox = new TextBox();  
myInkCanvas.Children.Add(myTextBox);  
myTextBox.Visibility = Visibility.Visible;  
Point mousePos = e.GetPosition(myInkCanvas);  
double left = Math.Min(mouseDownPos.X, mousePos.X);  
double top = Math.Min(mouseDownPos.Y, mousePos.Y);  
myTextBox.Width = Math.Abs(mouseDownPos.X - mousePos.X);  
myTextBox.Height = Math.Abs(mouseDownPos.Y - mousePos.Y);  
InkCanvas.SetLeft(myTextBox, left);  
InkCanvas.SetTop(myTextBox, top);  
}  

you can get mouseDownPos in :

private void myInkCanvas_PreviewMouseLeftButtonDown(objectsender,MouseButtonEventArgs){  
mouseDownPos = e.GetPosition(myInkCanvas);  
}

This is a simple code that you can use it in your program.

0

There is a regular Children property: http://msdn.microsoft.com/en-us/library/system.windows.controls.inkcanvas.children.aspx

Check the examples for a code example

Zenuka
  • 3,110
  • 28
  • 39
  • Hi, Thanks. But looks like the positionning does not work. it always put it on top 100,100 even if of course i changed the parameters – user96547 Sep 30 '09 at 15:28
  • Could you edit you question and add some sample xaml and code-behind? – Zenuka Oct 01 '09 at 11:43