简体   繁体   中英

How to access a DataContext, that has been specified in XAML, from within code-behind?

I'm specifying my DataContext view model inside my XAML, as follows:

<Window.DataContext>
    <vm:SeasonDivisionViewModel />
</Window.DataContext>

If that SeasonDivisionViewModel class has a public property, is it possible to access this from within the code-behind?

I'm trying to learn and take one step at a time, and I'd like to avoid adding commands yet, and just populate data from within code-behind, for now.

If you meant to access the DataContext after the view loads on the code behind you can simply use this.

var viewModel = (MyViewModel)DataContext;

Where MyViewModel is the data type of your data context.

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