FrameworkElement 的 DataContext 属性不会沿元素树继承
WPF 专业人士您好,至少我希望你们中的一些人阅读本文!
DataContext 是 FrameworkElement(所有 WPF 控件的基类)上的一个属性,并作为 DependencyProperty 实现。这意味着逻辑树中的所有后代元素共享相同的 DataContext。
那么 ContentControl 应该与其后代元素一起执行此操作,对吗?
我有一个场景 NOT< /strong> 这个案例,我想知道这种不当行为的原因是什么?!
如果您对它有更多了解,请阅读此线程(不想复制此处的所有内容),问题从这里开始......:
WPF:找不到触发目标“cc”。目标必须出现在任何 Setters、Triggers 之前
,并用简短的话说出来:ContentControl 中的我的 DataTemplates 确实有一个死的 DataContext,这意味着没有任何东西可以绑定到它,什么是实际上不可能...
ContentControl 下的每个元素都没有在 DataContext 属性中设置任何内容???
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 DependencyProperty. That means all the descendant elements in the logical tree share the same DataContext.
So the ContentControl should do it with its descendant elements right?
I have a scenario where that is NOT the case and I would like to know WHAT is the cause of that misbehaviour ?!
That you understand a bit more about it please read this thread ( dont NOT want to copy everything here) where the trouble starts...:
WPF: Can not find the Trigger target 'cc'. The target must appear before any Setters, Triggers
and to say it in short words: My DataTemplates within the ContentControl do have a dead DataContext that means there is NOTHING to bind to it, what is actually not possible...
Every Element down the ContentControl has NOTHING set in the DataContext Property ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它是依赖属性这一事实并不意味着继承...对于
DataContext
来说是这样,但这只是因为依赖属性在其元数据中具有FrameworkPropertyMetadataOptions.Inherits
标志。ContentControl
有点特殊:其后代的DataContext
(从DataTemplate
构建的可视化树)实际上是Content<
ContentControl
的 /code>。因此,如果您的ContentControl
没有内容,则其中的DataContext
为 null。The fact that it's a dependency property doesn't imply inheritance... It's true for
DataContext
, but only because the dependency property has theFrameworkPropertyMetadataOptions.Inherits
flag in its metadata.ContentControl
is a bit special: theDataContext
of its descendants (the visual tree built from theDataTemplate
) is actually be theContent
of theContentControl
. So if yourContentControl
has no content, theDataContext
inside it is null.这对我有用:
没有
Content="{Binding}"
,DataContext 为 NULLThis worked for me:
Without the
Content="{Binding}"
, the DataContext was NULL最后一个答案(来自 VinceF)也对我有用。
我想根据视图模型中属性的值显示用户控件。所以我用一些样式触发器制作了一个ContentControl。根据绑定属性的值,触发器设置包含特定用户控件的特定 ContentTemplate。
用户控件显示正确,但其 DataContext 始终为空。因此,我必须将 ContentControl 的上下文设置为:
Content="{Binding}"
之后,UserControls 工作正常并且具有与其父级相同的 DataContext。所以我的 XAML 看起来像这样:
在资源部分我定义了两个 DataTemplates;我想显示的每个用户控件的每一个。
我根据属性显示 UserControl 的部分如下:
The last answer (from VinceF) worked for me too.
I wanted to show a usercontrol depending on the value of a property in my viewmodel. So I made a ContentControl with some Style Triggers. Depending on the value of a bind property the trigger sets a specific ContentTemplate containing the specific usercontrol.
The usercontrol was shown right, but its DataContext was always null. So I had to set the Context of the ContentControl to:
Content="{Binding}"
After that, the UserControls worked fine and had the same DataContext as their parent.So my XAML looks like that:
In the Resources part I defined two DataTemplates; each one for each UserControl I want to show.
The part where I show the UserControl depending on a property is the following:
阅读此问题和之前的答案后,我更喜欢将 ContentControl 与数据触发的内容一起使用,如下所示:
将被设置为 ContentControl 内容的控件:
ContentControl 通过 DataTrigger 以 ContentControl 样式切换自己的内容:
我希望这对像以前的答案那样的人有帮助我。
after reading this question and previous answers, I prefer using ContentControl with data triggered Content like this:
Controls which will be set as Content of ContentControl:
ContentControl which switch own content by DataTrigger in ContentControl style:
I hope this helps to someone like previous answers to me.