Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 986999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:24:34+00:00 2026-05-16T05:24:34+00:00

trying my first attached behavior: I want to bind the TextSelection of the RichTextBox

  • 0

trying my first attached behavior: I want to bind the TextSelection of the RichTextBox to my ViewModel`s property:

public TextSelection SelectedRichText {get;set;}

That way I bind it:

<RichTextBox behavior:RichTextBoxSelectionBehavior.RichTextBoxSelection="{Binding SelectedRichText}" />

Thats my code and I have 2 questions:

1) Why is the OnRichTextBoxSelectionPropertyChanged never called?
2) see the question is this method at bottom: OnRichTextBoxGotSelectedText

public static class RichTextBoxSelectionBehavior
    {

        public static TextSelection GetRichTextBoxSelection(DependencyObject obj)
        {
            return (TextSelection)obj.GetValue(RichTextBoxSelection);
        }

        public static void SetRichTextBoxSelection(DependencyObject obj, TextSelection value)
        {
            obj.SetValue(RichTextBoxSelection, value);
        }

        // Using a DependencyProperty as the backing store for MyProperty.      
        public static readonly DependencyProperty RichTextBoxSelection =
            DependencyProperty.RegisterAttached
            (
                "RichTextBoxSelection", 
                typeof(TextSelection),
                typeof(RichTextBoxSelectionBehavior),
                new UIPropertyMetadata(OnRichTextBoxSelectionPropertyChanged)
            );

        private static void OnRichTextBoxSelectionPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
        {
            RichTextBox rtb = dpo as RichTextBox;

            if (rtb != null)
            {
                if ( !((TextSelection)args.NewValue).IsEmpty)
                {
                    // if the TextSelected has selected text hook up the RichTextBox intenal SelectedChanged event with my own
                    rtb.SelectionChanged += OnRichTextBoxGotSelectedText;
                }
                else
                {
                    rtb.SelectionChanged -= OnRichTextBoxGotSelectedText;
                }
            }
        }

        private static void OnRichTextBoxGotSelectedText(object sender, RoutedEventArgs e)
        {
            RichTextBox rtb = (RichTextBox) sender;

            // How can I pass now my rtb.Selection to the property the behavior is bound to? e.g. my SelectedRichText property in the ViewModel

            //Action action = () => { rtb.Selection; };
            //rtb.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:24:34+00:00Added an answer on May 16, 2026 at 5:24 am

    I mixed up an attached behavior with a dependency property, here is the solution:

    Put a RichTExtBox in a UserControl and this code too:

         // SelectedText property. 
                public static readonly DependencyProperty SelectedTextProperty =
                    DependencyProperty.Register("SelectedText", typeof(TextSelection),
                    typeof(MyRichTextBox ));
    
          /// <summary>
                /// Default constructor.
                /// </summary>
                public MyRichTextBox ()
                {
                    InitializeComponent();
    
                    this.TextBox.SelectionChanged += new RoutedEventHandler(TextBox_SelectionChanged);
                }
    
                void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
                {
                    var sT = (e.OriginalSource as RichTextBox).Selection;
                    SelectedText = sT;
                }
    
    
    
    
       /// <summary>
        /// The WPF Selected Text of the FlowDocument in the control
        /// </summary>
        public TextSelection SelectedText
        {
            get { return (TextSelection)GetValue(SelectedTextProperty); }
            set { SetValue(SelectedTextProperty, value); }
        } 
    
        //UserControl embedded in the MainWindow.xaml
          <My:MyRichTextBox SelectedText="{Binding SelectedDocument,Mode=TwoWay}" x:Name="EditBox" />
    

    Now you have access to the TextSelection of the richtextbox in your ViewModel!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to retrieve First Unique set of 2 attributes. I am looking
This is my first time trying to set up a database. My intent is
I'm trying to read the texture attached to a first FBO (fboA), modifying it
Hi I am trying to print out the first element and the element attached
I'm trying to write a model method that will pull topics attached to a
Ok, after failing to read a polynomial, I'm trying first a basic approach to
I am an absolutely newb to GAE and am trying my first sample application.
First time trying to deal with users logging in with ASP.net and haven't had
I was trying for the first time to read data from a file, a
this is my first time trying to use document classes in AS3 and im

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.