2011-06-02 84 views
3

如何将datatemplate中的textbox文本属性绑定到ContentControl Content属性?
(不通过的ElementName bindng)将数据绑定到ContentControl的内容

这是我的代码(即不工作):

<Window.Resources> 
    <DataTemplate x:Key="Temp"> 
     <TextBox TextWrapping="Wrap" Text="{TemplateBinding Content}" Height="20" Width="Auto"/>   
    </DataTemplate> 
</Window.Resources> 
<Grid> 
    <ContentControl ContentTemplate="{DynamicResource Temp}" Content="1"/>   
</Grid> 

回答

5

使用相对源绑定:

Text="{Binding RelativeSource={RelativeSource AncestorType=ContentControl}, Path=Content}" 

编辑:我可能应该注意的是,就绑定目标而言,这相当于{Binding},因为ContentTemplate中的DataContextContent

但是直接绑定到DataContext不会回传播到源DataContext,因此ContentControlContent在使用时不改变这种结合(或双向兼容的变化{Binding .},改变绝对没有如据我所知)。