|
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
namespace Slex.Lib.Interactions
{
/// <summary>
/// A helper class for registering dependency properties, and to fire notifications
/// </summary>
public class DependencyPropertyHelper
{
/// <summary>
/// Listen for change of a property of the source, and feedback the same to the context via the callback
/// </summary>
/// <param name="propertyPath"></param>
/// <param name="element"></param>
public static System.Windows.DependencyProperty RegisterForNotification<T>(DependencyObject context, object source,
string propertyPath, PropertyChangedCallback callback)
{
return RegisterForNotification<T>(context, source, propertyPath, callback, false);
}
/// <summary>
/// Listen for change of a property of the source, and feedback the same to the context via the callback
/// </summary>
/// <param name="propertyPath"></param>
/// <param name="element"></param>
public static System.Windows.DependencyProperty RegisterForNotification<T>(DependencyObject context, object source,
string propertyPath, PropertyChangedCallback callback, bool twoWay)
{
//Bind to a depedency property
Binding b = new Binding(propertyPath) { Source = source, Mode = BindingMode.OneWay };
if (twoWay) b.Mode = BindingMode.TwoWay;
var prop = System.Windows.DependencyProperty.RegisterAttached(
"Listener" + propertyPath + DateTime.Now.Ticks,
typeof(object),
typeof(T),
new System.Windows.PropertyMetadata(callback));
BindingOperations.SetBinding(context, prop, b);
return prop;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#