Reputation: 31
What are the differences between the following ways of assigning DataContext
in a UserControl
?
DataContext = this;
(Content as FrameworkElement).DataContext = this;
Upvotes: 1
Views: 242
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