子控件不会通过样式从父控件继承DataContext

问题描述

我有这样的东西:

class CustomControl : Control 
{ 
}

具有这种样式:

<Style targettype="{x:Type CustomControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate targettype="{x:Type CustomControl}">
                <Border x:Name="PART_Host"  />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

最初访问DataContextPART_Host中的CustomControl的{​​{1}}很好。 现在,当我更改DataContext中的CustomControl时,它不会传递给PART_Host。我通过手动绑定样式中的DataContext解决它。

DataContext={TemplateBinding DataContext}

但这似乎很奇怪。我希望DataContext会被传递给它的可视子对象,并且由于依赖属性没有Inherit标志,因此也会对其进行更新。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)