This project is read-only.

External Property Dependencies

Declarative Usage

The declarative PDFx API allows you to register a dependency on a property that resides in any INotifyPropertyChanged-instance different from the owner of the dependent Property:

class SourceClass : INotifyPropertyChanged
{
	private AnyType _sourceProperty;
	public AnyType SourceProperty
	{
		get { return _sourceProperty; }
		set
		{
			_sourceProperty = value;
			OnPropertyChanged("SourceProperty");
		}
	}
}

class DestinationClass : BindableExt
{
	SourceClass Source { get; set; }

	public AnyOtherType TargetProperty
	{
		get
		{
			Property(() => TargetProperty)
				.Depends(p => p.On(Source, k => k.SourceProperty));

			//Source.SourceProperty has changed. Do Something with it and return.
		}
	}
}

Overloads of the methods On and AndOn both allow you to pass in an INotifyPropertyChanged instance as the first parameter and point to any property of that instance in the Lambda Expression you pass in as the second parameter.

Example

The source code of this example can be found in ViewModel ExternalDependencyVM which is part of the WPFSample's source code.

Let's assume we are required to implement the following graph:

ExternalPropertyDependencies.png
Green circles stand for Input Properties while purple circles indicate calculated properties. The arrows show the underlying math operations as well as the property dependencies.

Using the PDFx, we can easily represent the depicted graph in C#:

public class DestinationVM : BindableExt
{
	public DestinationVM()
	{
		ExternalSource1 = new ExternalSource1();
		ExternalSource2 = new ExternalSource2();
	}

	public ExternalSource1 ExternalSource1 { get; private set; }
	public ExternalSource2 ExternalSource2 { get; private set; }

	public int A1
	{
		get
		{
			Property(() => A1)
				.Depends(p => p.On(() => B1)
						.AndOn(ExternalSource1, k => k.A1)
						.AndOn(ExternalSource2, k => k.A1));

			return B1 + ExternalSource1.A1 - ExternalSource2.A1;
		}
	}

	private int _b1;
	public int B1
	{
		get { return _b1; }
		set { _b1 = value; NotifyPropertyChanged(() => B1); }
	}
}

public class ExternalSource1 : BindableExt
{
	public int A1
	{
		get
		{
			Property(() => A1)
				.Depends(p => p.On(() => B1));

			return 3 * B1;
		}
	}

	private int _b1;
	public int B1
	{
		get { return _b1; }
		set { _b1 = value; NotifyPropertyChanged(() => B1); }
	}
}

public class ExternalSource2 : BindableExt
{
	private int _a1;
	public int A1
	{
		get { return _a1; }
		set { _a1 = value; NotifyPropertyChanged(() => A1); }
	}
}

Last edited May 10, 2013 at 6:50 PM by KevinStumpf, version 5

Comments

No comments yet.