0

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?

user853710
  • 1,715
  • 1
  • 13
  • 27
  • I had a similar problem, take a look at this thread - http://stackoverflow.com/questions/16416523/display-name-from-foreign-key-constraint-within-a-combobox Hope this helps concur your problem! :) – greg Jun 14 '13 at 10:30
  • Where do you put the getEmp method. I see no code for the ViewModel. – user853710 Jun 14 '13 at 10:49
  • Within your ViewModel, in your case the ModulesViewModel. Within that question, there are two links. Take a look through them as they might give you further help in regards to implementing what you are wanting to achieve :). Hope this helps. – greg Jun 14 '13 at 10:51
  • My problem is that I my ViewModel is not being initialized – user853710 Jun 14 '13 at 10:55
  • you are using the mvvm pattern right? – dennis schütz Jun 14 '13 at 11:15
  • Do I need maybe to create an instance of the viewmodel in code manually? – user853710 Jun 14 '13 at 12:14

1 Answers1

0

well in my solution I had a pretty simple codeline for that. I added a ListProperty for my foreign key value and filled this list in the loadData() method of my business class.

        private void LoadData(ef.table data)
    {
        // method to set all the properties
        //this.example = data.example;
        LoadProperty<ForeignKeyClass>(ForeignKeyProperty, ForeignKeyClass.Get(data.efForeignKeyTable));
    }

    //Property
    public static readonly PropertyInfo<ForeignKeyClass> ForeignKeyProperty = RegisterProperty<ForeignKeyClass>(c => c.ForeignKeyPropertyName);
    public ForeignKeyClass ForeignKeyPropertyName
    {
        get { return GetProperty<ForeignKeyClass>(ForeignKeyProperty); }
        private set
        {
            SetProperty<ForeignKeyClass>(ForeignKeyProperty, value);
        }
    }

now u have got a property which is filled with the foreignKey data in your model. don't know if this is helping you but, this is how I get my foreignKey data into my Model. good luck

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
dennis schütz
  • 383
  • 4
  • 21