I am experiencing this error when using WPF + XAML + MVVM in Visual Studio 2012.
Cannot resolve symbol ”MyVariable“ due to unknown DataContext
What is the solution?
I am experiencing this error when using WPF + XAML + MVVM in Visual Studio 2012.
Cannot resolve symbol ”MyVariable“ due to unknown DataContext
What is the solution?
This error is produced by ReSharper when designing XAML for WPF, and indicates that the XAML cannot find the class that contains run-time bindings. This usually indicates that the
DataContext
is not set properly.This error means that:
binding
in the XAML;For those of us that think in MVVM, this error indicates that the View cannot find the ViewModel.
Solution 1
Go through some sort of web tutorial to understand how DataBinding works. Recommend Microsoft Data Binding Overview.
Solution 2
If using ReSharper, pressing Alt-Enter on the offending DataContext will bring up a menu that helps you to insert the correct DataContext into your XAML.
I used this to correctly resolve the issue.
Solution 3
In the "Properties" pane of Visual Studio, you can select the data context for the selected control:
Solution 4
Blend can also be used to set the data context. Open up your .sln file in Blend, select the design element, then in the properties select "New":
Solution 5
DevExpress can also help to resolve this error in the XAML for you, using its wizard.
In the XAML, select the parent element you want to set the data context for (usually the entire form), then in the designer select the action triangle.
Then, browse to the class with the C# code.
Hint: The class will be invisible unless you add a parameterless constructor to the class.
XAML before
XAML after
Hint 6
If you cannot see the Smart Tags on the WPF designer, check that they have not been switched off at some point:
Solution 7
One can add call a snippet of code on startup that pops up a message box every time there is a binding error. This has turned out to be very useful.
In case the aforementioned web link goes down, here is the code:
Method:
Solution 8
Use the free utility Snoop.
There is a really nice feature that allows you to filter by controls with binding errors. This allows you to navigate straight to the visual with the binding error.
After starting Snoop:
Hint 9 - Design Time DataContext
There are actually two completely separate DataContexts:
design time
andrun time
.Most of the previous solutions are focused on setting the
run time
DataContext.Once you set the
design time
DataContext, the XAML preview in Visual Studio or Blend will show custom data provided by your custom C# class.If using Blend, this custom data can also be read from an XML file, but I prefer to provide it from my own C# class.
To set the
design time
DataContext, see:Or, add this to any element (this will new up the class
MyClass
at design time, so Intellisense will work):And this to the header:
Behind the scenes, when you set the
design time
DataContext:Note that the XAML preview only appears if you are using a User Control. If you prefer to use DataTemplates, no problem: you can create a temporary User Control that includes the DataTemplate, and set the
design time
DataContext to point at a static class. Code up the static class so that it creates a new instance of your ViewModel (i.e. the class you want to bind to). For example, your static class could read data from a database, populate the properties of the ViewModel, and you could work with live data from the database at XAML design time.This technique also works perfectly well with Dependency Injection, such as Unity or MEF. You have to point your
design time
DataContext at a static class which grabs the appropriate classes out of the dependency injection container, and sets everything up. You can then see live data at design time in the XAML preview. The aforementioned links demo how this works (complete with YouTube videos of a live ticking clock at XAML design time!).Needless to say, this technique works perfectly well with the MVVM pattern, and also with MVVM + Dependency Injection. For those of you unfamiliar with MVVM, its a great way to produce elegant, clean, maintainable and easy-to-alter projects. Microsoft Blend itself is entirely written using the MVVM pattern.