Mrainy
Mrainy

Reputation: 65

Add handler to the images in RichTextBox in Silverlight

In the Silverlight , we can insert an image in a RichTextBox by using a InlineUIContainer. Now I'm trying to add some handlers to this image. Here is my code:

InlineUIContainer contain = new InlineUIContainer();
Image image = new Image();
ImageSource img = new BitmapImage(new Uri(Source,UriKind.RelativeOrAbsolute));
image.SetValue(Image.SourceProperty, img);
image.MouseEnter +=new MouseEventHandler(image_MouseEnter);
container.Child = image;
rtb.Selection.Insert(contain);

However, when we move our mouse enter this image, nothing happen. What I'm trying to add are some handlers like resize, click, and drag and drop. Is it possible? I appreciate for any answers. Thanks!

Upvotes: 0

Views: 1045

Answers (3)

shane
shane

Reputation: 415

Also, I came up with a new solution to my same problem last night. I check out if I'm clicking (RTB_OnLeftMouseDown) on one of my images based on looping through all images in the BlockControl and looking at the block control's viewRect. Then I go into ReadOnly mode while I resize until I click off of the image again (go back to edit mode). This allows me to put little resize adorners onto the image and get all of the mouse events on the image. It's a pretty complicated solution though, so you might want to go a different route.

Upvotes: 1

shane
shane

Reputation: 415

The reason nothing happens is because in EDIT mode (IsReadOnly == false) on a richtextbox no events fire inside the richtextbox. You can get around this but it's complicated. You put the event handlers on the RichTexTBox, then you get the visual rectangle of the images in the richtextbox and see if the mouse event args point is inside a image.

Edit better explanation of why you can't: http://forums.silverlight.net/forums/p/224490/541921.aspx

Upvotes: 0

brunnerh
brunnerh

Reputation: 184376

The FlowDocument of RichTextBoxes is disabled, hence no events are processed, see this question of mine for more info and a possible workaround.

Upvotes: 0

Related Questions