简体   繁体   中英

Does DataTemplate in the resources set the datacontext of the view automatically?

in my window resources i have a datatemplate like the following

<DataTemplate DataType="{x:Type ViewModels:GoogleViewModel}">
            <Views:GoogleView/>
</DataTemplate>

does that mean if i bind the content of a UI elemnt to that view model it will load the view and set the datacontext of that view to the view model automatically, or i still have to set the datacontext for the view?

Yes that will set the dataContext of the Viewmodel to the View Automatically. Always DataContext set to View not to the viewModel. That means Object of the ViewModel will be bind to the View not the other way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM