Silverlight 4 C# - How to show a popup at text location from custom usercontrol?
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
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
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.
精彩评论