Thursday, August 30, 2012

RichTextBox editor determine if selection is image

RichTextBox editor determine if selection is image

I have an editor similar to the one described here:

http://msdn.microsoft.com/en-us/library/ff426926(v=vs.95).aspx

I am trying to determine if the user's selection in the rtb is an image so they can edit the properties without deleting it and re-adding it.

I have already implemented this feature for a hyperlink with this code:

private void RichTextBoxEditor_SelectionChanged(object sender, RoutedEventArgs e)          {              Inline parent = this.RichTextBoxEditor.Selection.Start.Parent as Inline;              if (parent != null)              {                  Hyperlink hyperlink = parent.ElementStart.Parent as Hyperlink;                  if (hyperlink == null)                  {                      this.tbUrlEditor.Visibility = Visibility.Collapsed;                  }                  else                  {                      this.tbUrlEditor.Visibility = Visibility.Visible;                      this.tbUrlEditor.Text = hyperlink.NavigateUri.ToString();                  }              }            }

So, when the user's caret or selection is a hyperlink, they are able to edit its Uri in my TextBox "tbUrlEditor".

I am hoping to be able to do the same thing with an image, but I am not successful when I try the same approach.

Answers & Comments...

Answer: 1

bherdrick

So, when the user's caret or selection is a hyperlink, they are able to edit its Uri in my TextBox "tbUrlEditor".

I am hoping to be able to do the same thing with an image, but I am not successful when I try the same approach.

Hi bherdrick,

You can try to set a breakpoint at ( parent.ElementStart.Parent as Image) to find if there is retured an Image or not, it there is, you may try get the image's size or name.

Best Regards,



Answer: 2

Unfortunately, that doesn't work.  The breakpoint isn't hit when I try that.  The xaml that is generated for the hyperlink is in this form:

<Paragraph>      <Run FontFamily="Portable User Interface" FontSize="12" Text="Some text ">        <Run.Foreground>          <SolidColorBrush Color="#FF000000" />        </Run.Foreground>      </Run>      <Hyperlink FontSize="12" NavigateUri="/someuri.aspx" TargetName="_self" TextDecorations="Underline">        <Hyperlink.Foreground>          <SolidColorBrush Color="#FF01439A" />        </Hyperlink.Foreground>        <Run FontFamily="Portable User Interface" FontSize="12" Text="some uri text">          <Run.Foreground>            <SolidColorBrush Color="#FF01439A" />          </Run.Foreground>        </Run>        <Hyperlink.MouseOverForeground>          <SolidColorBrush Color="#FFED6E00" />        </Hyperlink.MouseOverForeground>      </Hyperlink>      <Run FontFamily="Portable User Interface" FontSize="12" Text="more text. ">        <Run.Foreground>          <SolidColorBrush Color="#FF000000" />        </Run.Foreground>      </Run>    </Paragraph>  

and for the image:

<Paragraph TextAlignment="Center">      <InlineUIContainer>        <Image Height="132" Source="http://www.mysite.com/myimage.png" Stretch="Uniform" Width="600" />      </InlineUIContainer>    </Paragraph>

When the image is selected in the RichTextBox, the "parent" is null.  I tried both 

Image imgParent = this.RichTextBoxEditor.Selection.Start.Parent as Image;
InlineUIContainer imgParent = this.RichTextBoxEditor.Selection.Start.Parent as InlineUIContainer;
Neither of those work either.





Answer: 3

Hi bherdrick,

What is the relative complete code of your RichTextBoxEditor? To troubleshoot this issue, we need the source code to reproduce the problem, so that we can investigate the issue in house. It is not necessary that you send out the complete source of your project. You can remove any confidential information or business logic from it. You can upload the project in the skydrive and post the link here.

Best Regards,



Answer: 4

I sent you the link to it through a pm.





No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog