FrameworkElement 的 DataContext 属性不会沿元素树继承

发布于 2024-10-08 18:06:12 字数 730 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假扮的天使 2024-10-15 18:06:12

DataContext 是一个属性
FrameworkElement(所有元素的基类
WPF 控件)并实现为
依赖属性。这意味着所有
逻辑中的后代元素
树共享相同的 DataContext。

它是依赖属性这一事实并不意味着继承...对于 DataContext 来说是这样,但这只是因为依赖属性在其元数据中具有 FrameworkPropertyMetadataOptions.Inherits 标志。

所以 ContentControl 应该做到这一点
及其后代元素对吗?

ContentControl 有点特殊:其后代的 DataContext(从 DataTemplate 构建的可视化树)实际上是 Content< ContentControl 的 /code>。因此,如果您的 ContentControl 没有内容,则其中的 DataContext 为 null。

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.

舞袖。长 2024-10-15 18:06:12

这对我有用:

<ContentControl ContentTemplate="{StaticResource NotesTemplate}"
                Content="{Binding}"
                DataContext="{Binding HeightField}"/>

没有 Content="{Binding}",DataContext 为 NULL

This worked for me:

<ContentControl ContentTemplate="{StaticResource NotesTemplate}"
                Content="{Binding}"
                DataContext="{Binding HeightField}"/>

Without the Content="{Binding}", the DataContext was NULL

孤千羽 2024-10-15 18:06:12

最后一个答案(来自 VinceF)也对我有用。

我想根据视图模型中属性的值显示用户控件。所以我用一些样式触发器制作了一个ContentControl。根据绑定属性的值,触发器设置包含特定用户控件的特定 ContentTemplate。

用户控件显示正确,但其 DataContext 始终为空。因此,我必须将 ContentControl 的上下文设置为: Content="{Binding}" 之后,UserControls 工作正常并且具有与其父级相同的 DataContext。

所以我的 XAML 看起来像这样:

在资源部分我定义了两个 DataTemplates;我想显示的每个用户控件的每一个。

<DataTemplate x:Key="ViewA">
    <namespace:UserControlA/>
</DataTemplate>
<DataTemplate x:Key="ViewB">
    <namespace:UserControlB/>
</DataTemplate>

我根据属性显示 UserControl 的部分如下:

<ContentControl Content="{Binding}">
    <ContentControl.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Property}" Value="0">
                    <Setter Property="ContentControl.ContentTemplate" Value="{StaticResource ViewA}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Property}" Value="1">
                    <Setter Property="ContentControl.ContentTemplate" Value="{StaticResource ViewB}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>

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.

<DataTemplate x:Key="ViewA">
    <namespace:UserControlA/>
</DataTemplate>
<DataTemplate x:Key="ViewB">
    <namespace:UserControlB/>
</DataTemplate>

The part where I show the UserControl depending on a property is the following:

<ContentControl Content="{Binding}">
    <ContentControl.Style>
        <Style>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Property}" Value="0">
                    <Setter Property="ContentControl.ContentTemplate" Value="{StaticResource ViewA}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=Property}" Value="1">
                    <Setter Property="ContentControl.ContentTemplate" Value="{StaticResource ViewB}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>
素染倾城色 2024-10-15 18:06:12

阅读此问题和之前的答案后,我更喜欢将 ContentControl 与数据触发的内容一起使用,如下所示:

将被设置为 ContentControl 内容的控件:

<TextBox x:Key="ViewA">
   ...
</TextBox>
<ComboBox x:Key="ViewB">
   ...
</ComboBox>

ContentControl 通过 DataTrigger 以 ContentControl 样式切换自己的内容:

<ContentControl>
  <ContentControl.Style>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Property}" Value="0">
                <Setter Property="Content" Value="{StaticResource ViewA}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=Property}" Value="1">
                <Setter Property="Content" Value="{StaticResource ViewB}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
  </ContentControl.Style>
</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:

<TextBox x:Key="ViewA">
   ...
</TextBox>
<ComboBox x:Key="ViewB">
   ...
</ComboBox>

ContentControl which switch own content by DataTrigger in ContentControl style:

<ContentControl>
  <ContentControl.Style>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Property}" Value="0">
                <Setter Property="Content" Value="{StaticResource ViewA}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=Property}" Value="1">
                <Setter Property="Content" Value="{StaticResource ViewB}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
  </ContentControl.Style>
</ContentControl>

I hope this helps to someone like previous answers to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文