ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

IsInDesign

Provides a mechanism to wrap WPF user control code that causes an exception on the host WPF window. Notes: I use a separate DLL for all of my extensions which can cause additional challenges. Here are some tips; There are 3 assemblies required 2 of which are easy to add unless you know there nuances; "System.Windows.Controls" this requires "PresentationCore" & "PresentationFramework". "System.Windows" was the most interesting. Look for WindowsBase.DLL if you don't find a reference. Be sure it matches the .Net Framework version you are using..

Source

namespace ExtensionMethods
{
    public static class WPFUserControlExtensions
    {
        public static bool IsInDesignMode(this UserControl uc)
        {
            return ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)) ;
        }
    }
 
}

Example

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }
 
    //this is only an example but is a valid illustration
	//in the host WPF window you get an 
	// System.Exception... unable to open database file
	// by using the "extension IsInDesignMode" the control loads fine
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
            if (!this.IsInDesignMode())
            {
                SQLiteDatabase db = new SQLiteDatabase("..\\..\\Data\\ComboBoxItems.db");
                if (db != null)
                {
                    string query =
                        "Select * From ComboBoxItems";
                    m_gridRowItems = db.GetDataTable(query);
                }
            
                if (GridRowItems != null)
                    GenericGrid.ItemsSource = new DataGridCollectionView(GridRowItems.DefaultView);
            }

        }
    }
}

Author: Tom Kelly

Submitted on: 12 feb. 2014

Language: C#

Type: UserControl

Views: 5657