Reputation: 7777
In the xaml code i get an error telling cannot create an instance of this AllEmployeeViewModel class file, actually this class file exists in the solution folder when i type scr: the intellsene shows me the class file
<UserControl.Resources>
<scr:AllEmployeeViewModel x:Key="empName"></scr:AllEmployeeViewModel>
</UserControl.Resources>
<Grid x:Name="MainGrid" Background="White" Width="400"
Height="407" DataContext="{Binding Source={StaticResource empName}}" >
<Grid x:Name="grdAllEmp" DataContext="{Binding Path=EmployeeClass}">
<sdk:DataGrid AutoGenerateColumns="True" Height="274"
HorizontalAlignment="Left" Margin="8,8,0,0"
Name="dgEmployee" VerticalAlignment="Top" Width="385"
ItemsSource="{Binding}"/>
<Button Content="Get All Employees" Height="23"
HorizontalAlignment="Left" Margin="12,288,0,0"
Name="btnAllEmplloyees" VerticalAlignment="Top" Width="381"
Command="{Binding Path=DataContext.GetEmployees,ElementName=MainGrid}"/>
</Grid>
i am trying to bind the data to grid, if i ignore the compile time error and run its gives an error key not found.
please let me know the solutionif you know,working on this issue from past 2days
any help would be great thanks.
Upvotes: 1
Views: 4827
Reputation: 880
I was getting the same error, i will explain it to you hopefully it will help you. In my ViewModel's constructor i was executing some code, all code was withing below if condition except,
If(!IsInDesignMode) { // my code } // Problamatic method execution point
Except a method that i wanted to execute every time, but it turns out that you can only execute code if above condition is satisfied else your view model instance will not be created.
So to avoid this you have to do like that:
If(!IsInDesignMode) { // my code // Problamatic method execution point }
Put all your code inside that condition and everything will be fine.
Note: I was using MVVMLight library along with Model-View-ViweModel pattern.
Upvotes: 0
Reputation: 41
I too had the same issue.
cannot create instance of viewmodel
Just copy this code and place it in ViewModel
public bool IsDesignTime
{
get
{
return (Application.Current == null) ||
(Application.Current.GetType() == typeof(Application));
}
}
//Constructor
public ViewModelClass()
{
if(IsDesignTime == false)
{
//Your Code
}
}
Upvotes: 4
Reputation: 7777
Just add this line in MainPage.xaml.cs page
InitializeComponent();
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
//Code that throws the exception
}
it works fine.
Upvotes: 2
Reputation: 93621
You have a binding to EmployeeClass
. That has to be a collection of some type for this to work, but the name EmployeeClass
sounds like a single object and not a collection.
You really needed to post your View Model code as we had to guess this.
I put together a quick example and if the ViewModel contains:
public ObservableCollection<EmployeeClass> Employees { get; set; }
and I populate them with a few sample EmployeeClass objects,
public AllEmployeeViewModel()
{
this.Employees = new ObservableCollection<EmployeeClass>();
this.Employees.Add(new EmployeeClass() { Name = "One" });
this.Employees.Add(new EmployeeClass() { Name = "Two" });
and I change the binding to:
<Grid x:Name="grdAllEmp" DataContext="{Binding Path=Employees}">
It looks like this (no other changes):
Upvotes: 0
Reputation: 65054
Does your class AllEmployeeViewModel
have a zero-argument constructor? WIthout that, Silverlight cannot create an instance of your class.
Upvotes: 0