0

I deleted the CodeBehind of my MainWindow.xaml, cause I'm doing a small project where I literally must do that.

So I'm creating an instance of my ViewModel in this way over xaml:

<Grid.DataContext>
    <lib:StartPageViewModel />
</Grid.DataContext>

Well now, I need this DataContext in my Code (StartPageViewModel), as I want to open an other solution (For more Informations take a look here).

Any Ideas, on how I can get this DataContext?

Community
  • 1
  • 1
eMi
  • 5,540
  • 10
  • 60
  • 109
  • you can define a static resource separately with value lib:StartPageViewModel in xaml and then you can bind the same resource in grid and with new property in view model so that both will have the same object. – D J Sep 11 '12 at 10:02

3 Answers3

1

At least I solved the problem. Actually I didn't really need to use the DataContext for this:

    public static DTE2 GetDTE(DataContext dataContext)
    {
        ICustomTypeDescriptor typeDescriptor = dataContext as ICustomTypeDescriptor;
        Debug.Assert(typeDescriptor != null, "Could not get ICustomTypeDescriptor from dataContext. Was the Start Page tool window DataContext overwritten?");
        PropertyDescriptorCollection propertyCollection = typeDescriptor.GetProperties();
        return propertyCollection.Find("DTE", false).GetValue(dataContext) as DTE2;
    }

I changed the code to the following, it works now perfectly, I can open Solutions without using the DataContext:

    public static DTE2 GetDTE()
    {
        return (DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE");
    }
eMi
  • 5,540
  • 10
  • 60
  • 109
0

A ViewModel shouldn't know anything about the View.

So when applying 'proper' MVVM, you shouldn't be trying to get to the DataContext from within the ViewModel.

The code of the StartPageViewModel IS part of the object that is put in the DataContext. This means that you can access the object by using this in the code of the ViewModel.

If you are looking for the Grid (or even higher up the VisualTree) you could pass it using a property in the Xaml:

<Grid Name="MyGrid">
    <Grid.DataContext>
        <lib:StartPageViewModel MyParent={Binding ElementName=MyGrid} />
    </Grid.DataContext>
</Grid>

Still, if you do that, you are adding knowledge about the View to the ViewModel.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • `this` is the `StartPageViewModel`, could you tell me how to access the DataContext by using `this`? As I can only access the Properties now.. – eMi Sep 11 '12 at 10:30
  • What do you want to do with the DataContext? – Emond Sep 11 '12 at 13:18
0

I've never declared a viewmodel in a XAML file, but have you tried:

viewName.DataContext as ViewModelType;

BTW, deleting the code-behind is usually a good practice.

Noich
  • 14,631
  • 15
  • 62
  • 90
  • The View is in an other project, so I have no access to the Viewname, as this would mean, I have circulation dependencies. The ViewModel is in my other Project and mustn't have a reference to the XAML Project.. – eMi Sep 11 '12 at 10:32