Silverlight 4 C# - 如何在自定义用户控件的文本位置显示弹出窗口?

发布于 2024-10-13 19:33:47 字数 254 浏览 1 评论 0原文

我正在尝试在我的应用程序中创建拼写检查功能,该功能将在列表框中弹出可能的更正信息。

我还没有找到任何关于如何创建然后引用自定义用户控件,或者如何获取特定文本片段的坐标的任何合适的文档。

有没有人有一些示例代码,介绍如何制作带有弹出窗口的自定义用户控件,以及如何从主应用程序引用并显示该弹出窗口?另外,计算出文本框中某些选定文本的 X 和 Y 偏移量也很棒,这样我就可以将弹出窗口放置在拼写错误的右侧和正下方。

提前致谢!

-苏塔

I'm trying to make a spellcheck function in my application that will show a pop-up of possible corrections in a listbox.

I've not found any decent documentation on how to create and then reference a custom usercontrol, or how to get the coordinates of a specific piece of a bit of text.

Does anybody have some example code on how to make a custom usercontrol with a popup in it and how to then reference and show that popup from the main app? Also figuring out the X and Y offsets of some selected text in a textbox would be great also so I can position the popup to the right and just below the spelling error.

Thanks in advance!

-Sootah

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

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

发布评论

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

评论(2

如梦初醒的夏天 2024-10-20 19:33:47

假设您有一个名为 richtextbox 的 richttextbox 控件。然后,当您选择一些文本并按下按钮时,您可以创建一个弹出控件,其中包含您的自定义用户控件。然后确定文本相对于视觉根的位置,瞧:)

    richtTextBox.Selection.Text =
         "Lorem ipsum pro falli dicunt volumus te, ex velit probatus corrumpit per. His ex reque aperiam alienum, liber indoctum per an. Sed ei nibh cibo minim, et eam graeci suavitate. Vim iusto gubergren repudiandae ei.";

private void testButton_Click(object sender, RoutedEventArgs e)
{
    var rect = richtTextBox.Selection.Start.GetCharacterRect(System.Windows.Documents.LogicalDirection.Forward);
    var richtTextBoxPosition =
        richtTextBox.TransformToVisual(Application.Current.RootVisual).Transform(new Point(0, 0));

    var popup = new Popup()
                    {
                        HorizontalOffset = richtTextBoxPosition.X + rect.X,
                        VerticalOffset = richtTextBoxPosition.Y + rect.Y + richtTextBox.FontSize,
                        Height = 150,
                        Width = 100
                    };
    popup.Child = new ListBox()
    {
        Background = new SolidColorBrush(Colors.Red),
        Height = 150,
        Width = 100
    }; // use your custom UC here
    popup.IsOpen = true;
}

然后在您想要的解决方案中将选择手动设置为错误的文本。

http://strugglesofacoder.blogspot.com /2011/02/show-popup-under-text-in-rich-textbox.html

Imagine you have a richttextbox control called richtextbox. Then when you select some text and press a button you can create a popup control with your custom UserControl in it. Then determin the position of the text relative to your visual root and voila :)

    richtTextBox.Selection.Text =
         "Lorem ipsum pro falli dicunt volumus te, ex velit probatus corrumpit per. His ex reque aperiam alienum, liber indoctum per an. Sed ei nibh cibo minim, et eam graeci suavitate. Vim iusto gubergren repudiandae ei.";

private void testButton_Click(object sender, RoutedEventArgs e)
{
    var rect = richtTextBox.Selection.Start.GetCharacterRect(System.Windows.Documents.LogicalDirection.Forward);
    var richtTextBoxPosition =
        richtTextBox.TransformToVisual(Application.Current.RootVisual).Transform(new Point(0, 0));

    var popup = new Popup()
                    {
                        HorizontalOffset = richtTextBoxPosition.X + rect.X,
                        VerticalOffset = richtTextBoxPosition.Y + rect.Y + richtTextBox.FontSize,
                        Height = 150,
                        Width = 100
                    };
    popup.Child = new ListBox()
    {
        Background = new SolidColorBrush(Colors.Red),
        Height = 150,
        Width = 100
    }; // use your custom UC here
    popup.IsOpen = true;
}

Then in the solution you want is to set the selection manually to the errored text.

http://strugglesofacoder.blogspot.com/2011/02/show-popup-under-text-in-rich-textbox.html

旧伤还要旧人安 2024-10-20 19:33:47

您可能想要弹出的是 子窗口。默认情况下,此控件在显示时会在当前控件上创建一个覆盖层,并带有一个小的可自定义弹出窗口。

出于您的目的,我建议通过直接从 ChildWindow 控件继承来创建新的自定义控件。有一个关于子窗口的使用/创建的小教程 在这里。为了让它出现在您的文本中,您需要确定拼写错误所在位置的某种坐标,然后将其传递到您的 ChildWindow 并适当设置边距。

What you're likely looking for to make the pop up is a ChildWindow. This control by default creates an overlay over your current control when shown, and a small customizable popup with it.

For your purposes, I'd recommend creating your new custom control by inheriting directly from the ChildWindow control. There's a small tutorial on the usage/creation of a child window here. To get it to appear at your text, you'll need to determine some sort of coordinates of where the spelling error is, then pass it into your ChildWindow and set the margins appropriately.

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