简体   繁体   English

将DataContext从ContentControl传播到依赖项属性

[英]Propagate DataContext to Dependency Property from ContentControl

I have a styled ContentControl with a DependencyProperty which, among others, has a DataTemplate property (StatusTemplate) that I have to assign to a DataTemplate I got in resources (StatusTemplate1). 我有一个带有DependencyProperty的样式化ContentControl,它具有一个DataTemplate属性(StatusTemplate),必须将其分配给我在资源(StatusTemplate1)中获得的DataTemplate。

The ContentControl is shown and the 'Binding:: ' text from StatusTemplate1 is also displayed, but the binding is empty. 显示了ContentControl,还显示了StatusTemplate1中的'Binding ::'文本,但是绑定为空。 If I'm not mistaken, ContentControl doesn't propagate its DataContext to their Content, so the question is: is there any workaround I could use in order StatusTemplate1 receives the DataContext ? 如果我没有记错,ContentControl不会将其DataContext传播到其Content,所以问题是:为了让StatusTemplate1接收DataContext,我可以使用任何替代方法吗? I'd prefer to use XAML only for this, but I've got no problem if code-behind is the way to go. 我只想为此使用XAML,但是如果要使用代码隐藏,那我没有问题。

EDIT: the problem here is how to propagate the DataContext to the dependency property StatusTemplate. 编辑:这里的问题是如何将DataContext传播到依赖项属性StatusTemplate。 I changed the question title as it seems to be misleading. 我更改了问题标题,因为它似乎具有误导性。

The DataTemplate: 数据模板:

<DataTemplate x:Key="StatusTemplate1">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Binding:: " />
        <TextBlock Text="{Binding}" />
    </StackPanel>
</DataTemplate>

And the ContentControl: 和ContentControl:

<ContentControl Style="{Binding Path=Status, Converter={StaticResource StatusToStyleConverter}}" 
    dp1:AddOn.StatusTemplate="{StaticResource StatusTemplate1}">
</ContentControl>

Thanks 谢谢

I have a very similar scenario, and if I recall correctly, 我有一个非常相似的场景,如果我没记错的话,

Content="{Binding}"

As an attribute of the contentcontrol actually works. 作为contentcontrol的属性,它实际上可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM