I have a datatemplate controled control which is is being generated based on a EF collection of data which is set as the Datacontext of this control. The template has a also a combobox inside which should make it possible to change the foreign key value to another EF entityobject.
I am trying to load the foreign key collection from a viewModel
<my:EntryControlBase.Resources>
<my:ModulesViewModel x:Key="viewModel"/>
</my:EntryControlBase.Resources>
This is the control in a datatemplate
<DataTemplate>
<Grid Name="grdRoles" Background="Gray" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<TextBox Height="23" Text="{Binding Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
*<ComboBox Height="25" ItemsSource="{Binding Source={StaticResource viewModel}, Path=ModulesCollection}"* >
</ComboBox>
</Grid>
</DataTemplate>
And the viewmodel code behind.
public class ModulesViewModel :DependencyObject
{
public ModulesViewModel()
{
using(var context =new SPIS_Entities())
{
ModulesCollection = context.Module.Where(p => p.active).ToList();
}
}
public List<Module> ModulesCollection
{
get
{
return (List<Module>)GetValue(ModulesCollectionProperty);
}
set
{
SetValue(ModulesCollectionProperty, value);
}
}
public static readonly DependencyProperty ModulesCollectionProperty =
DependencyProperty.Register("ModulesCollection",
typeof(List<Module>),
typeof(ModulesViewModel),
null);
}
I was following a few examples from the internet, but I am having the whole time the problem that it can not find the "viewModel" ViewModel. it is set to null
The XAML definition of the viewmodel is set to the root of the usercontrol. Where ever I put it, it is underlined and null, saying "cannot create innstance ..." on mouse over. If I put it inside the combobox tags, it is still null, but not underlined
Suggestions?