简体   繁体   中英

Equivalent XAML line to this C#

In a WPF app, what XAML code do I need to do the same job as this line of c#:

this.DataContext = this;

? Thanks

<UserControl.DataContext>
    <Binding  Path="ViewModel"></Binding>
</UserControl.DataContext>

ViewModel is a public property in your code behind.

I think you could do:

DataContext="{Binding RelativeSource={x:Static RelativeSource.Self}}"

I've just tried it, and it seems to work...

EDIT: As noted in comments, apparently you can also use:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

... but I haven't tried that.

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