Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding question in WPF - Diference between Properties and Fields

Tags:

c#

binding

wpf

xaml

I have a question about how bindings work in WPF.

If i have a viewmodel with a property like this:

private string testString;
public string TestString
{
    get { return testString; }
    set { testString = value; }
}

Then if I bind it to a xaml with something like this:

<TextBlock
    Text="{Binding Path=TestString, Mode=TwoWay}"
    Foreground="Red"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    FontFamily="Calibri"
    FontSize="24"
    FontWeight="Bold">
</TextBlock>

It works... Nothing new here.

However, if I remove the getters and setters from the test string and end up with something like this:

public string TestString;

The very same binding doesn't work. I have no idea why this occurs because to me, it's equivalent a public attribute to a public attribute with custom get and set.

Can someone shed some light on this subject for me? :)

TYVM in advance!!

PS: Sorry about my syntax highlight. I just can't figure how to work with the code block.

like image 441
Silva Avatar asked Jun 11 '11 20:06

Silva


2 Answers

Removing the getter and setter changes the TestString member from being a property to being a field. This is why binding stops working and is probably not what you wanted to do (a public field like that is generally considered bad design).

You can let the compiler create the backing field automatically by declaring empty getter/setter, like this:

public string TestString { get; set; }
like image 194
Mårten Wikström Avatar answered Sep 30 '22 06:09

Mårten Wikström


@Silva, your hunch is correct, there is something going on behind the scenes. I've only seen this addressed clearly in one blog post I've come across on Pete Brown's (Microsoft Developer Evangelist) blog:

http://10rem.net/blog/2010/12/17/puff-the-magic-poco-binding-and-inotifypropertychanged-in-wpf

Check out the section on where he writes about the PropertyDescriptor class. He also goes on to mention that it is relatively inefficient compared to using the more conventional method of implementing the INotifyPropertyChanged interface on the viewmodel class, like this:

private string testString;

public string TestString
{
    get { return testString; }
    set {
        if (testString != value) {
            testString = value;
            RaisePropertyChanged("TestString");
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

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

I was pretty surprised to read that there's a 4x performance decrease by not implementing the interface.

like image 43
Kendrick Avatar answered Sep 30 '22 07:09

Kendrick