Giter Club home page Giter Club 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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

calculatedproperties's Issues

Calculated properties break property changed semantic

Let say we have this:

bool IsHorseVisible { get ; set }  // trigger property

bool IsCatVisible { get ; set }  // trigger property

bool IsAnimalVisible => Calculated(() => IsHorseVisible || IsCatVisible);

Initially we have this situation

IsHorseVisible = true;
IsCatVisible = true;

// IsAnimalVisible is true

Then we set:

IsHorseVisible = false;

// IsAnimalVisible will be raised cause source property value has changed

And so the property changed semantic is broken cause even if IsAnimalVisible value hasn't changed, the property is raised.

Support nested(child) property?

How to implement nested property notifications? Is this library supported?

[Why isn't my button enabling when text is added to the textbox with Fody.PropertyChanged?](https://stackoverflow.com/a/75983074/4380178)

This is a simple example, but sometimes I have to use nested properties (possibly with logic like ||, &&).

[AddINotifyPropertyChangedInterface]
public class Person
{
    public bool IsNameNotEmpty => !String.IsNullOrWhiteSpace(Name);
    public string? Name { get; set; }
}
[AddINotifyPropertyChangedInterface]
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public bool IsNameNotEmpty => SomePerson.IsNameNotEmpty;
    public Person? SomePerson { get; set; } = new();
}
IsEnabled="{Binding IsNameNotEmpty}"  // Can not notify!
IsEnabled="{Binding SomePerson.IsNameNotEmpty}" // Can notify!

c7OdC

Add .NET Standard 2.0 package

First, thanks for yet another useful library. Seems to work wonderfully, and really simplifies triggering INPC with calculated properties. While my preferred method so far is PropertyChanged.Fody (due to being essentially boilerplate-free), it doesn't work with .NET Standard F# projects (and there are F# edge cases it doesn't tackle that your library seems to handle well), and I need to ship soon, so I decided to give CalculatedProperties a try.

This seems to work fine with .NET Standard 2.0, but my build output is full of the following two warnings:

warning NU1701: Package 'Microsoft.Bcl 1.1.8' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
warning NU1701: Package 'Nito.CalculatedProperties 1.0.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.

I suppose this could be fixed if there was a .NET Standard 2.0 package for this on NuGet. I realize that you might not support this project any longer, what with it being over 2.5 years since the last commit, but I'd really appreciate it if you could. :)

Cross thread access in WPF

Can you please clarify the point in the wiki about cross thread access. It says that WPF does cross thread marshaling, but is there a workaround or does that mean that this library just can't be used with WPF?

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.