Git Product home page Git Product logo

calculatedproperties's Introduction

Logo

Calculated Properties

Easy-to-use calculated properties for MVVM apps (.NET 4, MonoTouch, MonoDroid, Windows 8, Windows Phone 8.1, Windows Phone Silverlight 8.0, and Silverlight 5).

.NET Core / ASP.NET vNext Status

AppVeyor Coveralls NuGet Pre Release

Support for IBindingList has been dropped in the .NET Core / ASP.NET vNext version.

Quick Start

Install the NuGet package.

Add a PropertyHelper instance to your view model (pass in a delegate that raises PropertyChanged for that instance):

private readonly PropertyHelper Property;

public MyViewModel()
{
    Property = new PropertyHelper(RaisePropertyChanged);
}

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

Next, create read/write trigger properties (pass the default value in the getter):

public string Name
{
    get { return Property.Get(string.Empty); }
    set { Property.Set(value); }
}

Now you can create read-only calculated properties:

public string Greeting
{
    get { return Property.Calculated(() => "Hello, " + Name + "!"); }
}

Done.

No, seriously. That's it.

MyValue and MyCalculatedValue will automatically raise PropertyChanged appropriately. Any time MyValue is set, both property values notify that they have been updated. This works even if they are properties on different ViewModels. The only thing you have to be careful about is to only access these properties from the UI thread.

It's magic!

How It Works

Property relationships are determined using dependency tracking. For more information, see the wiki.

The only really important thing you should know is that the PropertyChanged notifications are not raised immediately; they're deferred and then all raised together (combining any duplicate notifications).

You can defer notifications manually. You would want to do this, for example, if you are setting several different trigger values and want to do so in the most efficient manner:

using (PropertyChangedNotificationManager.Instance.DeferNotifications())
{
    vm.SomeProperty = someValue;
    vm.OtherProperty = otherValue;
    // At this point, no PropertyChanged events have been raised.
}
// At this point, all PropertyChanged events have been raised
//  (assuming there are no deferrals further up the stack).

This is especially useful if you have calculated properties that depend on multiple values that you're setting; by deferring the PropertyChanged notifications, you're consolidating the multiple PropertyChanged events into a single one.

Alternatives

Before writing this library, I looked pretty hard for something that already existed.

calculatedproperties's People

Contributors

stephencleary avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.