views:

48

answers:

3

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...

A: 

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

Thomas Levesque
I added some explanations. I am 'afraid' Vitali be right...
serhio
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;`...
serhio
A: 

You have to define it as DependencyProperty. In VS you can type in propd ant double tab to get it written for you.

Vitalij
No, you don't need it to be a dependency property. Only the *target* property of a binding needs to be a DP.
Thomas Levesque
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;`...
serhio
+1  A: 

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

SKG
So, finaly, what is the difference between a `DependancyProperty` and `INotifyPropertyChanged`..?
serhio
@serhio, see [this question](http://stackoverflow.com/questions/291518/inotifypropertychanged-vs-dependencyproperty-in-viewmodel)
Thomas Levesque