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 8157203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:12:08+00:00 2026-06-06T17:12:08+00:00

I am working on a WPF project. And I just created a dependency property.

  • 0

I am working on a WPF project. And I just created a dependency property. This dependency property is intended to make the RichTextBox.Selection.Text property bindeable.

But what I cannot do is get and set data to RichTextBox.Selection.Text using the same DP.

This is the code that I used if I ONLY want to get data from my RichTextBox.Selection.Text using binding:

public class MyRichTextBox: RichTextBox
{

   public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            "Text",
            typeof(string),
            typeof(MyRichTextBox),
            new PropertyMetadata(
                TextPropertyCallback));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    public MyRichTextBox()
    {         
        this.TextChanged += new TextChangedEventHandler(MyRichTextBox_TextChanged);
    }

    void MyRichTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        Text = this.Selection.Text;
    }

And it works perfectly, but with this code I cannot send any data from my ViewModel class.

So, if I ONLY want to set data to the RichTextBox.Selection.Text property from my ViewModel I used this code:

public class MyRichTextBox: RichTextBox
{
    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text",
        typeof(string),
        typeof(MyRichTextBox),
        new PropertyMetadata(
            TextPropertyCallback));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    private static void TextPropertyCallback(DependencyObject controlInstance, DependencyPropertyChangedEventArgs args)
    {
        MyRichTextBox instance = (MyRichTextBox)controlInstance;
        instance.Selection.Text = (String)args.NewValue;
    }

So, What Can I do if I want to be able to get and set data using the same dependency property???

Hope someone can help me, tahnk you in advance

  • 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-06-06T17:12:10+00:00Added an answer on June 6, 2026 at 5:12 pm

    Instead of binding your input control’s text to the VM’s property, you have binded an attached property to it, and in that attached property’s value changed you set the input control’s text.

    In other words – there is nothing monitoring the changed to the input control’s text.

    Edit:

    You are still doing:

    instance.Selection.Text = (String)args.NewValue;
    

    However, there is no notification of change to instance.Selection.Text.

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

Sidebar

Related Questions

I'm working on a WPF project, and my intention is to make two specific
I had been working on a WPF project for a while, but haven't touched
I'm working on a WPF project using Prism and MVVM, I'm new using this
I'm working on a WPF project using MVVM and I'm trying to implement a
I am working on a WPF project, and I am trying to fire an
I am working on a WPF project, using MVVM and Microsoft Prism libraries .
I am working on a WPF MVVM Project. Now i have a DataGrid in
I'm working on a multi-lingual WPF project that will be localized into many different
I have been working on a C# 4.0 WPF project and need to figure
I'm working on the UI side of a WPF project. My favourite reference while

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.