Skip to content

Instantly share code, notes, and snippets.

@dayneo
Created March 17, 2016 07:22
Show Gist options
  • Select an option

  • Save dayneo/e400051090a19faa9407 to your computer and use it in GitHub Desktop.

Select an option

Save dayneo/e400051090a19faa9407 to your computer and use it in GitHub Desktop.
Provides event based formatting and parsing. (WPF)
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
/// <summary>
/// Provides data for the Format and Parse events.
/// </summary>
public class ConvertEventArgs : EventArgs
{
/// <summary>
/// Creates a new instance of a ConvertEventArgs class.
/// </summary>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
public ConvertEventArgs(Type targetType, object parameter, CultureInfo culture)
{
this.TargetType = targetType;
this.Parameter = parameter;
this.Culture = culture;
}
Type _targetType;
/// <summary>
/// Gets the targetType value.
/// </summary>
public Type TargetType
{
get
{
return this._targetType;
}
private set
{
this._targetType = value;
}
}
object _parameter;
/// <summary>
/// Gets the parameter object.
/// </summary>
public object Parameter
{
get
{
return this._parameter;
}
private set
{
this._parameter = value;
}
}
CultureInfo _culture;
/// <summary>
/// Gets the culture to be used in the formatting of the value.
/// </summary>
public CultureInfo Culture
{
get
{
return this._culture;
}
private set
{
this._culture = value;
}
}
/// <summary>
/// Gets or sets the value.
/// </summary>
public object Value { get; set; }
}
/// <summary>
/// Represents the method that will handle the Parse and Format events of a EventedConverter.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public delegate void ConvertEventHandler(object sender, ConvertEventArgs e);
/// <summary>
/// Provides event based formatting and parsing.
/// </summary>
public class EventedConverter : IValueConverter
{
/// <summary>
/// Raised when a value is read from the model.
/// </summary>
public event ConvertEventHandler Format;
/// <summary>
/// Raises the Format event.
/// </summary>
/// <param name="e"></param>
protected void OnFormat(ConvertEventArgs e)
{
ConvertEventHandler handler = this.Format;
if (handler != null)
{
System.Diagnostics.Trace.TraceInformation("Formatting");
handler(this, e);
}
}
/// <summary>
/// Raised before the value is written to the model.
/// </summary>
public event ConvertEventHandler Parse;
/// <summary>
/// Raises the Parse event.
/// </summary>
/// <param name="e"></param>
protected void OnParse(ConvertEventArgs e)
{
ConvertEventHandler handler = this.Parse;
if (handler != null)
{
System.Diagnostics.Trace.TraceInformation("Parsing");
handler(this, e);
}
}
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
ConvertEventArgs e = new ConvertEventArgs(targetType, parameter, culture);
e.Value = value;
this.OnFormat(e);
return e.Value;
}
catch (Exception)
{
return DependencyProperty.UnsetValue;
}
}
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
try
{
ConvertEventArgs e = new ConvertEventArgs(targetType, parameter, culture);
e.Value = value;
this.OnParse(e);
return e.Value;
}
catch (Exception)
{
return DependencyProperty.UnsetValue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment