FrameworkElement`s DataContext Property does NOT inherit down the element tree

前端 未结 4 2072
情书的邮戳
情书的邮戳 2020-12-10 06:12

Hello WPF Pros at least I hope some of you read this!

DataContext is a property on FrameworkElement (base class for all WPF Controls) and is implemented as a Dep

4条回答
  •  暖寄归人
    2020-12-10 06:29

    DataContext is a property on FrameworkElement (base class for all WPF Controls) and is implemented as a DependencyProperty. That means all the descendant elements in the logical tree share the same DataContext.

    The fact that it's a dependency property doesn't imply inheritance... It's true for DataContext, but only because the dependency property has the FrameworkPropertyMetadataOptions.Inherits flag in its metadata.

    So the ContentControl should do it with its descendant elements right?

    ContentControl is a bit special: the DataContext of its descendants (the visual tree built from the DataTemplate) is actually be the Content of the ContentControl. So if your ContentControl has no content, the DataContext inside it is null.

提交回复
热议问题