Define a WPF control DataContext at design time

How to determine the DataContext specific control during development in a WPF application?

DataContext can be specifically installed in XAML , inherited or installed somewhere in the code, and sometimes it is difficult to determine the design time at which the class refers.

What I usually try to find in order to find the DataContext class is to search for the binding name in VS. For example, I see that binding is something like

 ItemSource = {Binding Items} 

... I will search for the text "Elements" to get to the class, but VS sometimes does not find the text I am looking for (I have several projects in the solution).

+6
source share
2 answers

DataContext of Control - ViewModel . Thus, there are many ways to set the ViewModel for the DataContext in the View , and if you find your ViewModel , but the ViewModel does not have the Items property, this means that you must add such a property to get the binding to work.

In addition, I recommend that you see the Debug->Windows->Output window, where you can see the binding information. That is, you may know binding errors.

In conclusion, I would like to show how to install ViewModel in a DataContext :

There are many approaches to setting a DataContext:

First approach. In sight:

 <Window.DataContext> <local:MainWindowViewModel/> </Window.DataContext> 

Second approach. You must override the OnStartUp() App.xaml.cs

 public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); MainWindow app = new MainWindow(); ProductViewModel context = new ProductViewModel(); app.DataContext = context; app.Show(); } } 

The third approach. In Windows Designer:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext=new MainWindowViewModel(); } } 

The fourth approach. You can set the DataContext via DependencyInjection to a UnityContainer or other IoC container. But DependencyInjection , Prism and UnityContainer are other issues and stem from this area of ​​the question. For instance:

 protected override void RegisterTypes() { unityContainer.RegisterType<object, ItemControl>("ModuleAUpper"); unityContainer.RegisterType<IViewModelItemControl, ViewModelItemControl>(); unityContainer.RegisterTypeForNavigation<ItemControl>(); } 
+1
source

I would like to add an approach to the StepUp list:

Design example:

Just as you can define a run-time data context, you can add a data context that is specifically designed for development time by:

  <Usercontrol x:Class="MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:viewModels="clr-namespace:MyProject.ViewModels" d:DataContext="{d:DesignInstance viewModels:MyViewModel}" d:DesignHeight="300" d:DesignWidth="600" mc:Ignorable="d"> </UserControl> 

In Visual Studio, you can use the IntelliSense properties for binding, and if your view model has a simple or even constructor without parameters, it will be created in the constructor, and you can test the trigger or converters without having to run the application.

+2
source

All Articles