0

In the following XAML, I am placing an InkCanvas on top of a RichTextBox control. In MVVM style, is there any way of returning the underlying RichTextBox object with the Mouse Events? (This so as to acquire the mouse position and closest word within the RichTextBox).

Thanks for any help or ideas.

<UserControl x:Class="Nova5.UI.Views.Ink.InkRichTextView"
           .....
   <Grid>    
         <f:SimpleRichTextBox Name="simplerichtextbox"  
         RichText="{Binding RichText, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
         >
        <FlowDocument PageWidth="{Binding ElementName=simplerichtextbox, Path=ActualWidth}" />
    </fsc:SimpleRichTextBox>

    <InkCanvas   
        Height="{Binding ElementName=simplerichtextbox, Path=ActualHeight}"
        Width="{Binding ElementName=simplerichtextbox, Path=ActualWidth}"
        Background="Transparent" 
        DefaultDrawingAttributes="{Binding Pen}" 
        EditingMode="{Binding EditingMode}" 
        Strokes="{Binding Strokes}" 
        h:MouseBehaviour.MouseUpCommand="{Binding MouseUpCommand}"
        h:MouseBehaviour.PreviewMouseDownCommand="{Binding PreviewMouseDownCommand}"/>
  </Grid>
</UserControl>
Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
  • It's possibly not pure MVVM but send the richtextbox to the command via a binding to the command parameter. – kidshaw Dec 08 '14 at 19:54
  • @piofusco Sorry, I don't get what your asking...How do you put an InkCanvas "inside" a RichTextBox?? – Alan Wayne Dec 08 '14 at 19:58
  • @kdshaw I'm thinking that or a OneWay to Source binding from the RichTextBox. I am hoping for a more elegant solution though. ?? – Alan Wayne Dec 08 '14 at 19:59
  • 2
    This isn't MVVM. Put your UI code in the codebehind, then expose your coordinates on the surface of the UserControl in DependencyProperties. Bind those to your VM. Done. –  Dec 08 '14 at 20:00
  • You could do an attached behaviour that binds to the mouse event of the attached object. That could expose a bindable command and probably do the nearest word lookup for you. It would be in view code but would satisfy MVVM using binding to a command with the value afterwards. More elegant but as with many things in wpf, more work. – kidshaw Dec 08 '14 at 20:02
  • @kidshaw I maybe wrong here, but the underlying RichTextBox does not receive any mouse events...??? – Alan Wayne Dec 08 '14 at 20:06
  • @Will Thanks. So simple I couldn't see it :) – Alan Wayne Dec 08 '14 at 20:08
  • Missed that. Would need to be above in the control hierarchy to receive the routed event. Looks like the user control and code behind is the best option. – kidshaw Dec 08 '14 at 20:55
  • @Will Having solved the mouse issue, I'm still stuck with getting the instance of RichTextBox out from the user control to apply things like GetPositionFromPoint(). Ideas?? – Alan Wayne Dec 08 '14 at 23:42
  • @Will Got it. Design ALL the UI code in the codebehind; then bind the items of interest (like highlighted words and paragraph) to the usercontrol to be pickedup by mainview? – Alan Wayne Dec 08 '14 at 23:51
  • Yep. This is all UI work, and therefore should live in the codebehind. VMs should contain your business logic, respond to changes in application state (e.g., coordinates, ICommand executions, etc). You can get a ref to your RTB via it's x:Name from codebehind. No biggie. –  Dec 09 '14 at 13:48

0 Answers0