擦除 InkCanvas 的子级

发布于 2024-11-15 23:40:05 字数 416 浏览 4 评论 0原文

我在窗口上有一个 InkCanvas,允许用户使用手写笔、触摸或鼠标进行绘图。我还允许用户添加文本。用户点击“添加文本”按钮,然后点击画布上他们想要添加文本的位置。那里会出现一个文本框,允许他们输入。在进入或失去焦点时,我创建一个 ContentControl 并将其添加到 myInkCanvas.Children

我希望用户能够在 InkCanvas 处于擦除模式时擦除他们创建的文本(我正在使用 EraseByPoint)。我尝试捕获内容控件的 MouseEnterPreviewMouseMove 事件,但似乎都没有触发。

有我可以捕捉的事件吗?有没有更好的方法来处理这种情况?有可能吗?

I have an InkCanvas on a window in which I allow the user to draw with a stylus, touch, or mouse. I also allow the user to add text. The user taps an "add text" button, then taps where on the canvas they would like their text. A textbox appears there, allowing them to type. On enter or lost focus I create a ContentControl and add it to myInkCanvas.Children.

I would like the user to be able to erase the text they have created when the InkCanvas is in erase mode (I am using EraseByPoint). I've attempted to capture the MouseEnter and PreviewMouseMove events of the content controls, but neither seem to fire.

Is there an event that I can capture? Is there a better way to handle this scenario? Is it even possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笙痞 2024-11-22 23:40:05

您可以使用命中测试来实现此目的
看看这里

首先获取InkPresenter

public T GetVisualChild<T>(Visual parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
            break;
    }
    return child;
}

InkPresenter inkPresenter = GetVisualChild<InkPresenter>(myInkCanvas);

,然后获取您的Point的HitTestResult

HitTestResult hitTestResult = VisualTreeHelper.HitTest(inkPresenter, new Point(x, y));

那么你可以使用 hitTestResult.VisualHit 删除这个对象

You can use hit testing for this purpose
look at here

get InkPresenter first

public T GetVisualChild<T>(Visual parent) where T : Visual
{
    T child = default(T);
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
            break;
    }
    return child;
}

InkPresenter inkPresenter = GetVisualChild<InkPresenter>(myInkCanvas);

then get HitTestResult of your Point

HitTestResult hitTestResult = VisualTreeHelper.HitTest(inkPresenter, new Point(x, y));

then you can use hitTestResult.VisualHit to remove this object

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文