j2601311
j2601311

Reputation: 31

WPF User Control's Data Context

What are the differences between the following ways of assigning DataContext in a UserControl?

  1. DataContext = this;
  2. (Content as FrameworkElement).DataContext = this;

Upvotes: 1

Views: 242

Answers (1)

Fratyx
Fratyx

Reputation: 5797

With DataContext = this; also the Content will 'inherit' the same DataContext. So for the Content there is no difference.

But with (Content as FrameworkElement).DataContext = this; you will not have a DataContext for the UserControl itself but only for the embedded FrameworkElement.

So if you intent to bind properties of the UserControl (Width, Height, Background) to the DataContext you have to take the first one.

Upvotes: 1

Related Questions