0

I have an object(class): Foo.

It has a property: Bar.

What should I do to be able to Bind to that 'Bar' property?

(WPF, .NET 4)

EDIT:

TO be more explicit, I give an example:

I have a Dot:UserControl

I create 2 properties of Dot - CenterX and CenterY:

    public double CenterX
    {
        get
        {
            return Canvas.GetLeft(this) + this.Width / 2;
        }
        set
        {
            Canvas.SetLeft(this, value - this.Width / 2);
        }
    }

    public double CenterY
    {
        get
        {
            return Canvas.GetTop(this) + this.Height / 2;
        }
        set
        {
            Canvas.SetLeft(this, value - this.Height / 2);
        }
    }

Now:

<Line Stroke="Black" x:Name="line1"
      X1="{Binding ElementName=dot1, Path=CenterX}"
      Y1="{Binding ElementName=dot1, Path=CenterY}"

      X2="{Binding ElementName=dot2, Path=CenterX}"
      Y2="{Binding ElementName=dot2, Path=CenterY}" />

does not work...

2
  • 1
    .NET 4 WPF or ASP.NET or WinForms? Where are you binding from? Is Foo an instance, or the class, and how can the place you're binding from get to Foo? Commented Sep 21, 2010 at 13:04
  • Sorry, forgot, this is for WPF Commented Sep 21, 2010 at 13:48

2 Answers 2

1

Implement INotifyPropertyChanged to your Class so that WPF binding framework will know to update the relevant bound controls when the property has been changed.

For the actual binding, you might find this useful

http://japikse.blogspot.com/2009/07/inotifypropertychanged-and-wpf.html

With INotifyPropertyChanged you class will look like

 public class Foo:INotifyPropertyChanged
    {
        public double CenterX
        {
            get
            {
                return Canvas.GetLeft(this) + this.Width / 2;
            }
            set
            {
                Canvas.SetLeft(this, value - this.Width / 2);
                OnPropertyChanged("CenterX");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Anytime CenterX value is changed, the OnPropertyChanged("CenterX"); will tell the UI to refresh controls which have properties bound to it

Sign up to request clarification or add additional context in comments.

2 Comments

So, finaly, what is the difference between a DependancyProperty and INotifyPropertyChanged..?
@serhio, see this question
0

Your question isn't precise enough to answer it completely... Unless specified otherwise, binding paths are always relative to the control's DataContext. Assuming your DataContext is an instance of Foo, you can do that:

<TextBlock Text="{Binding Bar}" />

Or if you want to be more explicit:

<TextBlock Text="{Binding Path=Bar}" />

Now, if Foo is itself a property of the current DataContext, you can do that:

<TextBlock Text="{Binding Foo.Bar}" />

Have a look at this Binding Cheat Sheet, it's very helpful when you're new to WPF binding

1 Comment

Given the example above, the generated dependency property generates me the code: return (double)GetValue(CenterXProperty);, but I need return Canvas.GetLeft(this) + this.Width / 2;...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.