简体   繁体   中英

SQL Server Compact + Entity Framework Design Time Data

I have an application being designed with SQL CE and Entity Framework. Is there a practical way to make that data available at design time to databound controls in Visual Studio Express 2012 for Desktop?

Assuming you're using an MVVM framework such as Caliburn.Micro, you can set the designer datacontext like so :

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:CaliburnDesignTimeData.ViewModels"
xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
mc:Ignorable="d" 
d:DataContext="{d:DesignInstance Type=vm:YourViewModel, IsDesignTimeCreatable=True}"
cal:Bind.AtDesignTime="True"

There are similar ways to do this using other MVVM frameworks.

Example:

public class YourViewModel : PropertyChangedBase
{
    public BindableCollection<Employee> Employees { get; set; }

    public YourViewModel
    {
        Employees = new BindableCollection<Employee>();

        if(Execute.InDesignMode)
        {
            // Add an employee when in design mode, this data will show up in design time
            Employees.Add(new Employee 
            {
                Name = "Sample Data Employee"
            });
        }
    }
}

Then bind it in XAML (if the designer datacontext added correctly, the VM's properties will even show up in Intellisense):

<Window
    ...
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:CaliburnDesignTimeData.ViewModels"
    xmlns:cal="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro"
    mc:Ignorable="d" 
    d:DataContext="{d:DesignInstance Type=vm:YourViewModel, IsDesignTimeCreatable=True}"
    cal:Bind.AtDesignTime="True"
    >
    <Grid>
        <DataGrid
              AutoGenerateColumns="True"
              ItemsSource="{Binding Employes}" />
    </Grid> 
</Window>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM