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>(); }