我正在使用一个InkCanvas,在其上显示一些自定义笔画(主要表示某些形状,例如Rectangle或RoundedRectangle)。我可以完美地绘制,选择,移动和调整这些形状的大小,但是现在我想在这些形状中添加一些文本。
问题是,自定义笔画无法容纳子项列表,因此我无法将TextBox添加到特定笔画。我尝试在InkCanvas子项中的特定位置(相对于笔触的位置)添加一个TextBox,但是结果非常糟糕,因为TextBox始终位于我的自定义笔划之后。
有什么办法吗?
谢谢
编辑:这是xaml代码
<InkCanvas ClipToBounds="True" Grid.Column="0" Grid.Row="0" Name="surfaceDessin"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
MouseLeave="surfaceDessin_MouseLeave" MouseMove="surfaceDessin_MouseMove" PreviewMouseMove="InkCanvas_LeftMouseMove" PreviewMouseUp="InkCanvas_LeftMouseUp" PreviewMouseDown="InkCanvas_LeftMouseDown"
Strokes="{Binding Path=Traits, Mode=OneTime}" EditingMode="{Binding Path=OutilSelectionne, Converter={StaticResource convertisseurModeEdition}, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"
DefaultDrawingAttributes="{Binding Path=AttributsDessin, Mode=OneTime}"/>
答案 0 :(得分:0)
由于我不知道C#到底发生了什么,+我做了一个小实验,看来您可以在自定义笔画类(我知道您有一个)中使用绘图上下文文字在您喜欢的位置。即使这不是一个完美的解决方案,我仍然认为比用文本框对其进行破解更好。总之,您只需要向自定义笔画类(drawingcontext.DrawText ....)添加一行即可。
PS:该代码主要来自其他来源,而不是我自己的全部来源。
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void DrawTextInRectangle(object sender, RoutedEventArgs e)
{
StylusPointCollection pts = new StylusPointCollection();
pts.Add(new StylusPoint(100, 100));
pts.Add(new StylusPoint(100, 300));
pts.Add(new StylusPoint(300, 300));
pts.Add(new StylusPoint(300, 100));
pts.Add(new StylusPoint(100, 100));
CustomStroke st = new CustomStroke(pts);
st.DrawingAttributes.Color = Colors.Red;
inkCanvas1.Strokes.Add(st);
}
}
}
// A class for rendering custom strokes
class CustomStroke : Stroke
{
Brush brush;
Pen pen;
public CustomStroke(StylusPointCollection stylusPoints)
: base(stylusPoints)
{
// Create the Brush and Pen used for drawing.
brush = new LinearGradientBrush(Colors.Red, Colors.Blue, 20d);
pen = new Pen(brush, 2d);
}
protected override void DrawCore(DrawingContext drawingContext,
DrawingAttributes drawingAttributes)
{
// Allocate memory to store the previous point to draw from.
Point prevPoint = new Point(double.NegativeInfinity,
double.NegativeInfinity);
// Draw linear gradient ellipses between
// all the StylusPoints in the Stroke.
for (int i = 0; i < this.StylusPoints.Count; i++)
{
Point pt = (Point)this.StylusPoints[i];
Vector v = Point.Subtract(prevPoint, pt);
// Only draw if we are at least 4 units away
// from the end of the last ellipse. Otherwise,
// we're just redrawing and wasting cycles.
if (v.Length > 4)
{
// Set the thickness of the stroke
// based on how hard the user pressed.
double radius = this.StylusPoints[i].PressureFactor * 10d;
drawingContext.DrawEllipse(brush, pen, pt, radius, radius);
prevPoint = pt;
}
}
Typeface tf = new Typeface("Arial");
FormattedText ft = new FormattedText("test test", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, tf, 15, Brushes.Red);
drawingContext.DrawText(ft, new Point(1.5 * prevPoint.X, 1.5 * prevPoint.Y));
}
}