Add a couple of useful converters

This commit is contained in:
Antony Male 2014-03-15 14:20:14 +00:00
parent 06fa5b646d
commit 5f51696c9f
3 changed files with 122 additions and 0 deletions

View File

@ -47,12 +47,14 @@
<Compile Include="AssemblySource.cs" />
<Compile Include="BindableCollection.cs" />
<Compile Include="BootstrapperBase.cs" />
<Compile Include="Xaml\BoolToVisibilityConverter.cs" />
<Compile Include="Xaml\CommandAction.cs" />
<Compile Include="Conductor.cs" />
<Compile Include="ConductorAllActive.cs" />
<Compile Include="ConductorBase.cs" />
<Compile Include="ConductorBaseWithActiveItem.cs" />
<Compile Include="ConductorOneActive.cs" />
<Compile Include="Xaml\DebugConverter.cs" />
<Compile Include="Xaml\EventAction.cs" />
<Compile Include="EventAggregator.cs" />
<Compile Include="Execute.cs" />

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace Stylet.Xaml
{
/// <summary>
/// Turn a boolean value into a Visibility
/// </summary>
public class BoolToVisibilityConverter : DependencyObject, IValueConverter
{
public static readonly BoolToVisibilityConverter Instance = new BoolToVisibilityConverter();
/// <summary>
/// Visibility to use if value is true
/// </summary>
public Visibility TrueVisibility
{
get { return (Visibility)GetValue(TrueVisibilityProperty); }
set { SetValue(TrueVisibilityProperty, value); }
}
// Using a DependencyProperty as the backing store for TrueVisibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TrueVisibilityProperty =
DependencyProperty.Register("TrueVisibility", typeof(Visibility), typeof(BoolToVisibilityConverter), new PropertyMetadata(Visibility.Visible));
/// <summary>
/// Visibility to use if value is false
/// </summary>
public Visibility FalseVisibility
{
get { return (Visibility)GetValue(FalseVisibilityProperty); }
set { SetValue(FalseVisibilityProperty, value); }
}
// Using a DependencyProperty as the backing store for FalseVisibility. This enables animation, styling, binding, etc...
public static readonly DependencyProperty FalseVisibilityProperty =
DependencyProperty.Register("FalseVisibility", typeof(Visibility), typeof(BoolToVisibilityConverter), new PropertyMetadata(Visibility.Collapsed));
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is bool))
return null;
return (bool)value ? this.TrueVisibility : this.FalseVisibility;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is Visibility))
return false;
var vis = (Visibility)value;
if (vis == this.TrueVisibility)
return true;
if (vis == this.FalseVisibility)
return false;
return null;
}
}
}

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
namespace Stylet.Xaml
{
/// <summary>
/// Converter which passes through values, but uses Debug.WriteLine to log them. Useful for debugging
/// </summary>
public class DebugConverter : DependencyObject, IValueConverter
{
public static readonly DebugConverter Instance = new DebugConverter();
/// <summary>
/// Category to use with Debug.WriteLine
/// </summary>
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
// Using a DependencyProperty as the backing store for Name. This enables animation, styling, binding, etc...
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(DebugConverter), new PropertyMetadata("DebugConverter"));
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter == null)
Debug.WriteLine(String.Format("Convert: Value = '{0}' TargetType = '{1}'", value, targetType), this.Name);
else
Debug.WriteLine(String.Format("Convert: Value = '{0}' TargetType = '{1}' Parameter = '{2}'", value, targetType, parameter), this.Name);
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter == null)
Debug.WriteLine(String.Format("ConvertBack: Value = '{0}' TargetType = '{1}'", value, targetType), this.Name);
else
Debug.WriteLine(String.Format("ConvertBack: Value = '{0}' TargetType = '{1}' Parameter = '{2}'", value, targetType, parameter), this.Name);
return value;
}
}
}