0

I have a class in the ViewModel folder called "MainViewModel", and I want my datacontext to be set to the class. I am doing it the following way, somehow it doesnt seem to work. Does anyone have some ideas? Thanks.

    xmlns:ViewModel="clr-namespace:***.***.ViewModel"
<MenuItem Header="always visible" DataContext="{Binding ViewModel:MainViewModel}"  IsCheckable="True" IsChecked="{Binding  MenuVisible}"  />

Many thanks.

2
  • To the class or to an object of the class? Commented Jul 26, 2011 at 18:02
  • What do you want the instance to come from? Commented Jul 26, 2011 at 18:03

2 Answers 2

1

As Tim has already noted, you are setting the class definition as your DataContext and not an instance. The example he gave sets the instance in XAML, which is perfectly accurate and gets the job done; however, in my experience you usually have the instance in your code-behind already. To set the DataContext, you would do something along the lines of:

myMenuItem.DataContext = myMainViewModelInstance;
Sign up to request clarification or add additional context in comments.

Comments

0

You're setting the class as your datacontext, not an instance of the class. Declare an instance as in the example in this question and bind to it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.