Reputation: 3241
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.
Upvotes: 0
Views: 445
Reputation: 504
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;
Upvotes: 1
Reputation: 12396
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.
Upvotes: 0