See UI changes in design view with WPF & XAML and data binding?

删除回忆录丶 提交于 2019-12-06 06:05:11
Contango

The DataContext allows the XAML to find an instance the class that it is meant to be binding to.

Then, the bindings in XAML allow you to bind to specific properties of said class.

There are two separate DataContexts: design time and run time.

To set the design time DataContext, see:

http://adamprescott.net/2012/09/12/design-time-data-binding-in-wpf/

Essentially, when you set the design time DataContext, behind the scenes the WPF runtime will automatically instantiate a new instance of the class you point it at (or simply point at the class if its static), and then the Visual Studio design time designer will display live values from your class, as you are editing the XAML. This makes designing really fast, as you can work with live data, and you dont have to run the program all the time to see how it looks.

To set the run time DataContext, see ReSharper WPF error: "Cannot resolve symbol "MyVariable" due to unknown DataContext". The answer describes how to use the free Snoop utility to detect runtime binding errors.

Added this code:

d:DataContext="{d:DesignInstance  local:Ball,IsDesignTimeCreatable=True}"

And now I can see my green ball at design time!

Thanks!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!