|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
using System.ComponentModel;
using System.Globalization;
namespace RadioPlayer {
public static class WPFExtensions {
[DebuggerStepThrough]
public static BindingExpressionBase SetBinding(this DependencyObject target, DependencyProperty dp, object source, string path) {
Binding b = new Binding(path);
b.Source = source;
return BindingOperations.SetBinding(target, dp, b);
}
[DebuggerStepThrough]
public static BindingExpressionBase SetBinding(this DependencyObject target, DependencyProperty dp, object source, string path, IValueConverter converter) {
Binding b = new Binding(path);
b.Source = source;
b.Converter = converter;
return BindingOperations.SetBinding(target, dp, b);
}
public static DispatcherOperation Dispatch(this DispatcherObject sender, Action callback) { return sender.Dispatch(DispatcherPriority.Normal, callback); }
public static DispatcherOperation Dispatch(this DispatcherObject sender, DispatcherPriority priority, Action callback) {
if (sender.Dispatcher == null) return null;
if (sender.Dispatcher.CheckAccess()) {
callback();
return null;
} else {
return sender.Dispatcher.BeginInvoke(priority, callback);
}
}
public static void AddValueChanged(this DependencyObject sender, DependencyProperty property, EventHandler handler) {
DependencyPropertyDescriptor.FromProperty(property, sender.GetType()).AddValueChanged(sender, handler);
}
public static void RemoveValueChanged(this DependencyObject sender, DependencyProperty property, EventHandler handler) {
DependencyPropertyDescriptor.FromProperty(property, sender.GetType()).RemoveValueChanged(sender, handler);
}
public static double ToRange(this double value, double minSource, double maxSource, double minTarget, double maxTarget) {
var sr = maxSource - minSource;
var tr = maxTarget - minTarget;
var ratio = sr / tr;
return minTarget+(value / ratio);
}
}
public class ValueConverter<TIN, TOUT> : IValueConverter {
public ValueConverter(Func<TIN, TOUT> forwardConversion) {
ForwardConversion = forwardConversion;
}
public ValueConverter(Func<TIN, TOUT> forwardConversion, Func<TOUT, TIN> reverseConversion) {
ForwardConversion = forwardConversion;
ReverseConversion = reverseConversion;
}
public Func<TIN, TOUT> ForwardConversion { get; set; }
public Func<TOUT, TIN> ReverseConversion { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
try {
var in1 = Object.ReferenceEquals(value, DependencyProperty.UnsetValue) ? default(TIN) : (TIN)value;
return ForwardConversion(in1);
} catch {
return Binding.DoNothing;
}
}
/// <summary>Implements <see cref="M:IValueConverter.ConvertBack"/>.</summary>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
try {
var out1 = Object.ReferenceEquals(value, DependencyProperty.UnsetValue) ? default(TOUT) : (TOUT)value;
return ReverseConversion(out1);
} catch {
return Binding.DoNothing;
}
}
}
}
|
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.
Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of course] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I used to work as a freelance architect, project manager, trainer, and consultant here, in Israel, but recently
join the company with extremely persuasive idea - to make a world better place. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them.
To be updated within articles, I publishing,
visit my blog or
subscribe RSS feed. Also you can
follow me on Twitter to be up to date about my everyday life.