Skip to content

Instantly share code, notes, and snippets.

@mfe-
Last active May 11, 2016 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfe-/8416ffc2bf5c41a83a06a0bd71ad31c4 to your computer and use it in GitHub Desktop.
Save mfe-/8416ffc2bf5c41a83a06a0bd71ad31c4 to your computer and use it in GitHub Desktop.
TypeConverter uwp?
//Dont forget to add the nugetpackage :System.ComponentModel+ :System.ComponentModel.TypeConverter
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace App2TypeConverters
{
[global::System.ComponentModel.TypeConverter(typeof(CarConverter))]
public class Car
{
private int _Name;
public int Name
{
get { return _Name; }
set { _Name = value; }
}
}
/// <summary>
/// https://github.com/dotnet/corefx/blob/d0dc5fc099946adc1035b34a8b1f6042eddb0c75/src/System.ComponentModel.TypeConverter/src/System/ComponentModel/BooleanConverter.cs
/// </summary>
public class CarConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string text = value as string;
if (text != null)
{
return new Car();
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is DateTime)
{
return "car";
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
<Page
x:Class="App2TypeConverters.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2TypeConverters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Background="Red">
<Page.Content>
<Grid Background="Transparent">
<ListBox>
<local:car></local:car>
</ListBox>
</Grid>
</Page.Content>
</Page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment