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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:17:29+00:00 2026-05-14T19:17:29+00:00

I am trying to do follow DataBinding Property -> DependencyProperty -> Property But i

  • 0

I am trying to do follow DataBinding

Property -> DependencyProperty -> Property

But i have trouble.
For example,
We have simple class with two properties implements INotifyPropertyChanged:

public class MyClass : INotifyPropertyChanged
    {
        private string _num1;
        public string Num1
        {
            get { return _num1; }
            set
            {
                _num1 = value;
                OnPropertyChanged("Num1");
            }
        }

        private string _num2;
        public string Num2
        {
            get { return _num2; }
            set
            {
                _num2 = value;
                OnPropertyChanged("Num2");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string e)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(e));
        }
    }

And TextBlock declared in xaml:

<TextBlock Name="tb" FontSize="20" Foreground="Red" Text="qwerqwerwqer" />

Now lets trying to bind Num1 to tb.Text:

private MyClass _myClass = new MyClass();
        public MainWindow()
        {
            InitializeComponent();

            Binding binding1 = new Binding("Num1")
                                   {
                                       Source = _myClass, 
                                       Mode = BindingMode.OneWay
                                   };

            Binding binding2 = new Binding("Num2")
            {
                Source = _myClass,
                Mode = BindingMode.TwoWay
            };

            tb.SetBinding(TextBlock.TextProperty, binding1);

            //tb.SetBinding(TextBlock.TextProperty, binding2);


            var timer = new Timer(500) {Enabled = true,};

            timer.Elapsed += (sender, args) => _myClass.Num1 += "a";

            timer.Start();


        }

It works well. But if we uncomment this string

tb.SetBinding(TextBlock.TextProperty, binding2);

then TextBlock display nothing. DataBinding doesn’t work! How can i to do what i want?

  • 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-14T19:17:30+00:00Added an answer on May 14, 2026 at 7:17 pm

    The problem is that the SetBinding call clears out any previous bindings. So when you set a binding to Num2, you are clearing out the binding to Num1. This happens because a dependency property binding cannot have multiple sources- how would it know which one to use? (Of course, this ignores the usage of a MultiBinding, but that’s not going to help you in this scenario).

    The way you can do this is to make MyClass a DependencyObject and Num1 and Num2 dependency properties. Then you can bind Num2 to the Text property of the TextBox, and Num2 will be updated whenever the text receives an update from Num1.

    A picture is worth a thousand words- what you’re trying to do is shown on the left. What you need to do is shown on the right:

    alt text http://img339.imageshack.us/img339/448/twosources.png

    Decided to try this out to ensure my logic was sound, and indeed it works, but there are some tricks. For starters, here is the new MyClass code:

    public class MyClass : FrameworkElement
    {
        public static readonly DependencyProperty Num1Property =
            DependencyProperty.Register("Num1", typeof(string), typeof(MyClass));
    
        public static readonly DependencyProperty Num2Property =
            DependencyProperty.Register("Num2", typeof(string), typeof(MyClass));
    
        public string Num1
        {
            get { return (string)GetValue(Num1Property); }
            set { SetValue(Num1Property, value); }
        }
    
        public string Num2
        {
            get { return (string)GetValue(Num2Property); }
            set { SetValue(Num2Property, value); }
        }
    }
    

    Nothing scary here, just replaced your INotifyPropertyChanged with DependencyProperty. Now let’s check out the window code-behind:

    public partial class DataBindingChain : Window
    {
        public MyClass MyClass
        {
            get;
            set;
        }
    
        public DataBindingChain()
        {
            MyClass = new MyClass();
    
            InitializeComponent();
    
            Binding binding1 = new Binding("Num1")
            {
                Source = MyClass,
                Mode = BindingMode.OneWay
            };
    
            Binding binding2 = new Binding("Text")
            {
                Source = tb,
                Mode = BindingMode.OneWay
            };
    
            tb.SetBinding(TextBlock.TextProperty, binding1);
            MyClass.SetBinding(MyClass.Num2Property, binding2);
    
            var timer = new Timer(500) { Enabled = true, };
    
            timer.Elapsed += (sender, args) => Dispatcher.Invoke(UpdateAction, MyClass);
    
            timer.Start();
        }
    
        Action<MyClass> UpdateAction = (myClass) => { myClass.Num1 += "a"; };
    }
    

    This is where the magic happens: we set up two bindings. The first binds the TextBlock.Text to Num1, the second binds Num2 to the TextBlock.Text. Now we have a scenario like the right side of the picture I showed you- a data-binding chain. The other magic is that we cannot update the Num1 property on a different thread from the one it was created on- that would create a cross-thread exception. To bypass this, we simply invoke an update onto the UI thread using the Dispatcher.

    Finally, the XAML used for demonstration:

    <Window x:Class="TestWpfApplication.DataBindingChain"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="DataBindingChain" Height="300" Width="300"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Name="tb" Grid.Row="0" FontSize="20" Foreground="Red"/>
        <TextBlock Name="tb2" Grid.Row="1" FontSize="20" Foreground="Blue" Text="{Binding MyClass.Num2}"/>
    </Grid>
    

    And voila! The finished product:

    alt text http://img163.imageshack.us/img163/6114/victorynf.png

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

Sidebar

Related Questions

I am trying to follow the example here ...but get the exception: 'System.Uri' does
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
So I was trying to follow http://developer.android.com/guide/topics/media/camera.html to create a simple app that captures
I'm trying to follow the steps found here on comparing two arrays, and knowing
Trying to follow the hints laid out here , but she doesn't mention how
Iam trying to follow the multithreading example given in: Python urllib2.urlopen() is slow, need
I'm trying to follow the advice of the sage Answermen re: moving my class
I'm trying to follow Michael Hartl's Ruby on Rails Tutorial in http://ruby.railstutorial.org/chapters/sign-in-sign-out , but
I'm trying to follow along with http://mongotips.com/b/array-keys-allow-for-modeling-simplicity/ I have a Story document and a
trying to follow example in the expert f# book, and having an issue with

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.