Nullable TypeConverter for Silverlight

by Justin Chase 26. January 2010 05:45

In Silverlight if you want a property of one of your Controls or Models to be Nullable<T> you will end up with a parser error without a little extra work. The reason for this is that there is no default TypeConverter for Nullable<T> so the parser doesn’t know how to convert the string in the Xaml to the appropriate type. To fix this you simply create your own TypeConverter and apply it to your property.

public class NullableTypeConverter<T> : TypeConverter
    where T : struct
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        Nullable<T> nullableValue = null;
        string stringValue = value as string;
        if (!string.IsNullOrEmpty(stringValue))
        {
            T converted = (T)Convert.ChangeType(value, typeof(T), culture);
            nullableValue = new Nullable<T>(converted);
        }

        return nullableValue;
    }
}

Then to apply it you simply do the following:

[TypeConverter(typeof(NullableTypeConverter<int>))]
public int? Example { get; set; }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | C# | Expression | Silverlight | WPF

Fixing Strange Rendering Artifacts for WPF in Win7

by Justin Chase 13. July 2009 08:43

In Win7 when viewing WPF applications (Expression Blend for example) I sometimes get strange rendering artifacts. It seems to be Win7 specific, or at least the drivers I have for Win7, since the same application can run on different environments and end up looking perfectly fine.

If you find yourself seeing strange things that don’t appear to be correct you can try adjusting these registry keys to see if it helps. I’m assuming these issues will be fixed by the Windows team / Video card manufacturers once Windows 7 is officially released but in the meantime this might help you resolve some of your own UI bugs.

Disclaimer: Do not edit your registry without backing up a copy first. Editing the registry can destroy your operating system if you don’t do it correctly, so use the below information at your own risk.

On Win7 x64:

Key Path: HKLM\SOFTWARE\Wow6432Node\Microsoft\Direct3D\
Key Name: DisableThreadedDDI
Key Type: DWORD
Key Value: 1

On Win7 x86:

Key Path: HKLM\SOFTWARE\Microsoft\Direct3D\
Key Name: DisableThreadedDDI
Key Type: DWORD
Key Value: 1

If you change these keys don’t forget to change them back after Windows 7 is officially released!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Expression | Misc | WPF | Windows

Silverlight 3 and Expression 3

by Justin Chase 11. July 2009 04:42

After months of top secret work at Microsoft my labors are nearly realized. Yesterday Silverlight 3 and Expression 3 were released as an RC. If you haven’t checked it out you should definitely go and play around with the new bits. Here is a overview from Soma himself:

http://blogs.msdn.com/somasegar/archive/2009/07/10/launching-silverlight-3-and-expression-studio-3.aspx

Specifically I worked on the Photoshop Import functionality found in Expressoin Blend, Design and Web. It has been an exciting and rewarding project and I’m really looking forward to what is coming up next!

One of the other cool features to check out which was created by fellow Minnesotans, is Sketch Flow for Expression Blend. It’s a cool tool you can use to quickly prototype new applications and features.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Microsoft | Expression | Photoshop Import

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

Justin Chase

sweetest hat ever

I am a software developer from St. Paul MN and I work for Microsoft on the Expression team. This blog is about various technical topics I find myself encountering here and there. In addition to loving WPF and Xaml and Expression studio in general I have a special interest in DSLs, programming languages and games.

RecentComments

Comment RSS

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar