views:

22

answers:

1

I placed a control into a grid. let's say the control is derived from public class 'ButBase' which is derived in its turn from System.Windows.Controls.Button. The code normally compiles and app works just fine. But there's something really annoying.

When you try to switch to xaml-design tab it will say 'The document root element is not supported by the visual designer', which is normal and I'm totally okay with that, but the thing is, that all the xaml code is underlined and VS2010 says: 'Cannot create an instance of ButBase' although still normally compiles and able to run.

I've tried the same code in VS2008, it said that needs to see a public parameterless constructor in the ButBase, and even after I put one it showed the same error.

What do I miss here?

+1  A: 

Make sure ButBase is not abstract. Designer does not like those.

Also make sure not to do any funny business in your constructor, Initialized and Loaded handlers. Funny includes any non-trivial tasks, such as connecting to a database or anything else that might interfere with the designer. Any such code should be wrapped with a designer-check. There are a number of ways out there to check for a presence of designer but I have found the following to work ok:

internal static class DesignModeChecker
{
    private static bool? _isInDesignModePriv;

    public static bool IsInDesignMode
    {
        get
        {
            if (!_isInDesignModePriv.HasValue)
            {
                _isInDesignModePriv = Process.GetCurrentProcess().ProcessName.ToLower().Trim() == "devenv" && !Debugger.IsAttached;
            }

            return _isInDesignModePriv.Value;
        }
    }
}
wpfwannabe