动态数据显示 - WPF - 需要将文本添加到画布 - C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13374270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic Data Display - WPF - Need to add text to canvas - C#
提问by Harsha
I am using the dynamic data display WPF chart. I have a requirement to display a label next to every point on the curves plotted on the chart.
我正在使用动态数据显示 WPF 图表。我需要在图表上绘制的曲线上的每个点旁边显示一个标签。
The exact functionality is as follows:
具体功能如下:
Every curve has a an object that holds its data and a description that inculdes color, marker shape etc. It also tell me whether the labels must be visible for that particular curve.
There is also an option using a checkbox to hide/show the labels for all points on all the curves on the plot.
There is a third option where a user can left click on the marker and see a label next to it.
每条曲线都有一个保存其数据的对象和一个包含颜色、标记形状等的描述。它还告诉我该特定曲线的标签是否必须可见。
还有一个选项使用复选框来隐藏/显示绘图上所有曲线上所有点的标签。
还有第三个选项,用户可以左键单击标记并查看其旁边的标签。
Now, I previously implemented it by adding labels along with the ElementMarkerPointGraph for each point and setting the visibility of the labels. I know there is a massive performance hit with this approach.
现在,我之前通过为每个点添加标签和 ElementMarkerPointGraph 并设置标签的可见性来实现它。我知道这种方法会对性能造成巨大影响。
I am now looking to create a solution where I can render text directly to the canvas at a location that I provide. I also need help with the removing the text from the canvas.
我现在正在寻找一个解决方案,我可以在我提供的位置将文本直接渲染到画布上。我还需要帮助从画布中删除文本。
Is there a way of adding text natively to the canvas? What is the most efficient way to do so?
有没有办法将文本本地添加到画布?这样做的最有效方法是什么?
EDIT: I need to move the text around as the plotter zooms. I already know when the plotter zooms, I need to be able to move the text to the appropriate location.
编辑:我需要在绘图仪缩放时四处移动文本。我已经知道绘图仪何时缩放,我需要能够将文本移动到适当的位置。
回答by hridya pv
I'm not sure whether this will give you the zooming purpose but the code below can be used to add text inside a canvas..I got it from a site while googling.
我不确定这是否会给你缩放目的,但下面的代码可用于在画布中添加文本......我在谷歌搜索时从网站上得到它。
private void Text(double x, double y, string text, Color color)
{
TextBlock textBlock = new TextBlock();
textBlock.Text = text;
textBlock.Foreground = new SolidColorBrush(color);
Canvas.SetLeft(textBlock, x);
Canvas.SetTop(textBlock, y);
canvasObj.Children.Add(textBlock);
}
回答by Harsha
OK. My exact implementation can't be put up here. But I can provide some idea of how to do it.
好的。我的具体实现不能放在这里。但我可以提供一些关于如何做到这一点的想法。
So create a simple user control that derives from Canvas.
因此,创建一个源自 Canvas 的简单用户控件。
class CustomCanvas : Canvas
{
protected override void OnRender(DrawingContext dc)
{
FormattedText someFormattedText = new FormattedText(someText, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
someTypeFace, someFontSize, someColor);
dc.DrawText(someFormattedText, new Point(15, 15));
}
}
You can seal the class, if you do not want it subclassed/overriden further.
如果您不希望它进一步被子类化/覆盖,您可以密封该类。
That's about it. You can check out the other methods available with the drawing context to do some other stuff. :)
就是这样。您可以查看绘图上下文中可用的其他方法来执行其他一些操作。:)
回答by Harsha
I figured it out myself. I'll be overriding the OnRender method to handle this. I can draw text using the drawing context.
我自己想通了。我将覆盖 OnRender 方法来处理这个问题。我可以使用绘图上下文绘制文本。

