• Stars
    star
    73
  • Rank 433,657 (Top 9 %)
  • Language
    C#
  • License
    MIT License
  • Created over 10 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy-to-use calculated properties for MVVM apps

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.

More Repositories

1

AsyncEx

A helper library for async/await.
C#
3,295
star
2

Comparers

The last comparison library you'll ever need!
C#
400
star
3

StructuredConcurrency

Structured concurrency support for C#
C#
203
star
4

Mvvm

MVVM helpers, including calculated properties and asynchronous notification tasks.
C#
132
star
5

AspNetBackgroundTasks

A component that registers "fire and forget" tasks with the ASP.NET runtime.
C#
109
star
6

Deque

Double-ended queue
C#
105
star
7

Disposables

IDisposable helper types.
C#
98
star
8

AsyncDiagnostics

Easy-to-use exception causality chains for async/await.
C#
61
star
9

BrowserBoss

Easily script browsers.
C#
55
star
10

Try

The Try monad (Error/Exceptional monad) for C#
C#
44
star
11

ConnectedProperties

Dynamically attach properties to (almost) any object instance.
C#
41
star
12

Guids

Helper types for creating and interpreting GUIDs.
C#
27
star
13

Presentations

Repository for presentations
C#
27
star
14

PegLeg

A PEG parser for C#, using code generators
C#
22
star
15

CrossDomainSingleton

An AppDomain-aware, thread-safe singleton that ensures only one instance exists in the entire application.
C#
22
star
16

LocalTelemetry

Local dashboards for telemetry collection.
18
star
17

Docs

Documentation of various and sundry matters
16
star
18

grounded-chatgpt

ChatGPT, but grounded in custom data
C#
16
star
19

HashAlgorithms

CRC calculation for .NET
C#
16
star
20

DependencyInjection

Helpers for Microsoft.Extensions.DependencyInjection.
C#
15
star
21

ArraySegments

A library for slicing and dicing arrays (without copying).
C#
13
star
22

Scripts

Misc single-file scripts and utilities
C#
13
star
23

ArcDb

An ACID .NET relational database
12
star
24

Mvvm.CalculatedProperties

Automatically raise PropertyChanged in MVVM applications.
C#
11
star
25

Cancellation

Types to assist with cancellation handling.
C#
11
star
26

DisplayProfiles

An application that saves and restores display monitor layouts.
C#
10
star
27

Mvvm.Core

Basic MVVM helpers.
C#
10
star
28

AsyncCTPUtil

Utility code originally distributed as part of the Async CTP
C#
10
star
29

ProjProps

A dotnet tool that displays project properties.
C#
10
star
30

Logging

Library for using scopes with Microsoft.Extensions.Logging.
C#
10
star
31

AsyncEx.Testing

Helper types for writing unit tests for asynchronous code.
C#
9
star
32

ProducerConsumerStream

A producer/consumer collection of bytes with a Stream-based API.
C#
8
star
33

OptionParsing

Flexible command-line parsing library
C#
8
star
34

BuildTools

Miscellaneous tools for building .NET Core libraries.
7
star
35

UniformResourceIdentifiers

Standard-compliant URI encoders and parsers
C#
6
star
36

iterjs

ES6 library for working with iterables and iterators
JavaScript
5
star
37

Quicly

An implementation of IETF QUIC in .NET
C#
4
star
38

StrongNamer

Simple app that strong-names NuGet packages.
C#
3
star
39

AsyncUnitTests

Helpers for async unit testing.
C#
3
star
40

blog-stephencleary-dotcom

blog.stephencleary.com
HTML
2
star
41

Hosting

.NET Generic Host for UI applications
C#
2
star
42

Collections.Async

Async-compatible producer/consumer collections.
2
star
43

comments.stephencleary.com

Comments for stephencleary.com.
JavaScript
2
star
44

stephencleary-dotcom

HTML
1
star
45

LoggerProviderHelpers

Helper types for implementing Microsoft's ILoggerProvider.
1
star
46

ARC

A simple dynamic language. Like JavaScript, but better!
C#
1
star
47

DockerLifetime

A .NET Core lifetime service for Docker containers
1
star
48

glob

GitHub Action that finds files using globs (wildcards)
JavaScript
1
star
49

Presentations-IntermediateAsync

Sample code for presentation given at GRDevDay 2013-03-02
1
star
50

Hymnals

Public domain hymnals
LilyPond
1
star