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

  • Home
  • SEARCH
  • 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 6387311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:08:11+00:00 2026-05-25T03:08:11+00:00

I am driving crazy with a custom Dependency Property. I already checked loads of

  • 0

I am driving crazy with a custom Dependency Property. I already checked loads of threads here, but haven’t found any solution yet. What I want to do is to replace the value of the Property if the source provides a specific value (null for the given example). No matter what I try, the property value within the source remains null and is never updated.

Here is my Custom Control:

public class TextBoxEx : TextBox
{
    public TextBoxEx()
    {
        TrueValue = 0;
        this.TextChanged += (s, e) =>
        {
            TrueValue = Text.Length;
            SetCurrentValue(MyPropertyProperty, TrueValue);
            var x = BindingOperations.GetBindingExpression(this, MyPropertyProperty);
            if (x != null)
            {
                x.UpdateSource();
            }
        };
    }

    public int? TrueValue { get; set; }

    public int? MyProperty
    {
        get { return (int?)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.Register("MyProperty", typeof(int?), typeof(TextBoxEx), new PropertyMetadata(null, PropertyChangedCallback));

    private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (e.NewValue == null)
        {
            d.SetCurrentValue(MyPropertyProperty, (d as TextBoxEx).TrueValue);
        }
    }
}

Here is the DataContext i am Binding:

public class VM : INotifyPropertyChanged
{
    private int? _Bar = null;

    public int? Bar
    {
        get { return _Bar; }
        set
        {
            _Bar = value;
            OnPropertyChanged("Bar");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

My Binding looks like this:

<local:TextBoxEx MyProperty="{Binding Bar, UpdateSourceTrigger=PropertyChanged}"/>

Remember: I need a TwoWay binding, so OneWayToSource does not work for me.

Any idea what I am not getting in here?

  • 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-25T03:08:12+00:00Added an answer on May 25, 2026 at 3:08 am

    You just need to set the binding to two-way and it will work. But as that should be the default you can register the property accordingly using the following metadata:

    ... new FrameworkPropertyMetadata(null,
                                      FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                      PropertyChangedCallback)
    

    Getting the expression in the TextChanged handler and updating source manually is not required so i would remove that code.


    If you do not explicitly set a mode on a binding the default will be used, from the documentation:

    Default: Uses the default Mode value of the binding target. The default value varies for each dependency property. In general, user-editable control properties, such as those of text boxes and check boxes, default to two-way bindings, whereas most other properties default to one-way bindings. A programmatic way to determine whether a dependency property binds one-way or two-way by default is to get the property metadata of the property using GetMetadata and then check the Boolean value of the BindsTwoWayByDefault property.

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

Sidebar

Related Questions

This one is driving me crazy. Here's the XAML: <UserControl x:Class=SilverlightApplication1.Page xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml> <Grid
This is driving me crazy. I'm not including any code because I want to
This is driving me crazy i know its a simple error but either i
This has been driving be crazy, but I can't seem to find an answer.
This is most def something very simple, but it is driving me crazy, cant
I'm driving myself crazy here, I'm almost positive I am at least on the
This is driving me crazy. I have this one php file on a test
This has been driving me crazy for a few days. Why doesn't the following
I've been driving myself crazy trying to get the Entity Framework to work as
This is driving me crazy. I'm not going to take it anymore. I'm going

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.