Nullable TypeConverter for Silverlight

by justin 26. January 2010 00: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; }

Tags: , , , ,

.NET | C# | Expression | Silverlight | WPF

Fixing Strange Rendering Artifacts for WPF in Win7

by justin 13. July 2009 03: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!

Tags: , , ,

Expression | Misc | WPF | Windows

Silverlight 3 and Expression 3

by justin 10. July 2009 23: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.

Tags: , ,

Microsoft | Expression | Photoshop Import

About Me

sweetest hat ever

I'm a software developer from Minnesota and this blog largely focuses on various technical concepts I am thinking about at the moment. I currently work for Microsoft in the St. Paul office of the Expression product group.

RecentPosts