[英]WP7 ContentPresenter passing DataContext to DataTemplate
I have a data item: 我有一个数据项:
<SampleData:Item Title="Evening News" Channel="ABC" x:Key="sampleData0" />
In my xaml page, I have a ContentPresenter that displays this item. 在我的xaml页面中,我有一个显示此项目的ContentPresenter。
<ContentPresenter ContentTemplate="{StaticResource dt1}" />
Here is the data template, dt1 : 这是数据模板, dt1 :
<DataTemplate x:Key="t2">
<Grid Background="#FF5599DD" DataContext="{StaticResource sampleData0}" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Channel}" Grid.Row="1" />
</Grid>
</DataTemplate>
This all works fine. 一切正常。 But I want to put the data template into my dictionary.xaml file, so I have to move the data context from the DataTemplate and into the ContentPresenter. 但我想将数据模板放入我的dictionary.xaml文件中,因此我必须将数据上下文从DataTemplate移动到ContentPresenter中。
Now my ContentPresenter looks like this: 现在我的ContentPresenter看起来像这样:
<ContentPresenter ContentTemplate="{StaticResource dt1}" DataContext="{StaticResource sampleData0}" />
And the data template looks like this: 数据模板如下所示:
<DataTemplate x:Key="t2">
<Grid Background="#FF5599DD">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Channel}" Grid.Row="1" />
</Grid>
</DataTemplate>
But this doesn't work! 但这不起作用!
I have experimented with RelativeSource but no luck. 我已经尝试过RelativeSource,但没有运气。
Anyone got any ideas? 有人有任何想法吗?
Instead of setting the DataContext
on the ContentPresenter
set the Content
property. 而不是在ContentPresenter
设置DataContext
,而是设置Content
属性。 Like this: 像这样:
<ContentPresenter Content="{StaticResource sampleData0}"
ContentTemplate="{StaticResource dt1}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.