Seeing design time data in a WPF control

Sometimes the ability to see the data at design time significantly facilitates the creation of WPF controls. If I am developing a WPF control with complex UI using data bound controls, I usually define a property for accessing some typical data item:

public static ComplexData DesignTimeItem
{
    get
    {
        using (DatabaseContext db = new DatabaseContext())
        {
            var products = from p in db.products.Include("ProductType")
                           where p.product_id == 131
                           select p;

            product product = products.First();

            return new ComplexData(product, "100 kg");
        }
    }
}

and then I use this property directly in XAML at design time:

<UserControl x:Class="SomeControl"
    ...
    xmlns:local="clr-namespace:ControlNamespace"
    DataContext="{Binding Source={x:Static local:ComplexData.DesignTimeItem}, Mode=OneWay}"
    ...
>

Then I need to do the final step. The problem is that the default entity context constructor (DatabaseContext) uses the connection string included in run time application configuration file app.config:

<connectionStrings>
  <add name="MyContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;password=******;User Id=myuser;server=myserver;database=mydb;Persist Security Info=True;charset=utf8&quot;" providerName="System.Data.EntityClient"/>
</connectionStrings>

but at design time the application configuration file is devenv.exe.config which doesn’t include the connection string and so the creation of my WPF control fails causing the designer to fail to load. To solve this problem I simply copy the connection string included in app.config to devenv.exe.config located in "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\" and restart Visual Studio.

1 Response to Seeing design time data in a WPF control

  1. Nilpesh patel on November 4, 2012 at 2:54 PM:

    Hi,

    Thanks…Since last few days, I have searched lot of Web Sites..But my prob of loading EF data @ Design time not soled..Now it is working..Thx..

Leave a Reply

Your email address will not be published. Required fields are marked *