Click here to Skip to main content
15,889,527 members
Articles / Desktop Programming / XAML

C# Property Class - Part 2

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
7 Feb 2010CPOL 21.1K   6   7
C# property class - Part 2

Commenter tonyt (from CodeProject) rightly points out that there are drawbacks to this approach: A C# Property Class.

Why:

Because there is a mountain of core functionality in .NET that relies heavily on things like property access via reflection (like data binding) and via System.ComponentModel (e.g., TypeDescriptor), none of which support your take on implementing properties.

You can implement INotifyPropertyChanged on any class that offers a more efficient way to get notified about property changes, as it requires only one delegate for each listener, regardless of how many supported properties you have.

He's right. But I still want to explore this approach. What if we take the Property class and have it implement INotifyPropertyChanged?

C#
public class Property<T> : INotifyPropertyChanged
{
    protected T _value = default(T);

    public Property()
    {
    }

    public Property(T value)
    {
        _value = value;
    }

    public event EventHandler Changed;

    public event PropertyChangedEventHandler PropertyChanged;

    public virtual T Value
    {
        get { return _value; }
        set
        {
            if ((value != null && !value.Equals(_value)) ||
                (_value != null && !_value.Equals(value)))
            {
                _value = value;
                OnChanged();                    
            }
        }
    }

    public override string ToString()
    {
        return object.ReferenceEquals(_value, null) ? string.Empty : _value.ToString();
    }

    protected virtual void OnChanged()
    {
        if (Changed != null)
            Changed(this, EventArgs.Empty);

        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("Value"));
    }

    public static implicit operator T(Property<t> property)
    {
        if (property == null)
            return default(T);

        return property.Value;
    }
}

Now we can create a class as follows:

C#
class ViewModel
{
    public ViewModel()
    {
        Status = new Property<string>("");
    }

    public Property<string> Status { get; private set; }
}

Note the switch from a readonly field to a read-only property. WPF binding requires properties and does not work with fields.

And then, we can bind to that in XAML as follows:

XML
<TextBox Text="{Binding Path=Status.Value}"/>

(assuming that the DataContext of the TextBox is a ViewModel like the one above.)

That should go a little way to addressing tonyt's point (mostly because WPF data binding is so robust).

This article was originally posted at http://spookycoding.blogspot.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader Starkey Laboratories
United States United States
The first computer program I ever wrote was in BASIC on a TRS-80 Model I and it looked something like:
10 PRINT "Don is cool"
20 GOTO 10

It only went downhill from there.

Hey look, I've got a blog

Comments and Discussions

 
GeneralThe PropertyChanged event call does not receive the correct property name Pin
Jim McCurdy9-Feb-10 13:09
Jim McCurdy9-Feb-10 13:09 
GeneralRe: The PropertyChanged event call does not receive the correct property name Pin
Don Kackman9-Feb-10 13:27
Don Kackman9-Feb-10 13:27 
QuestionIsnt this class available in .Net 4.0? Pin
theperm9-Feb-10 3:44
theperm9-Feb-10 3:44 
AnswerRe: Isnt this class available in .Net 4.0? Pin
Don Kackman9-Feb-10 8:59
Don Kackman9-Feb-10 8:59 
AnswerRe: Isnt this class available in .Net 4.0? [modified] Pin
Don Kackman9-Feb-10 12:24
Don Kackman9-Feb-10 12:24 
AnswerRe: Isnt this class available in .Net 4.0? Pin
Don Kackman9-Feb-10 12:31
Don Kackman9-Feb-10 12:31 
GeneralYes you can. Pin
tonyt8-Feb-10 1:47
tonyt8-Feb-10 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.