开发者

Binding datatemplate to ContentControl's Content

开发者 https://www.devze.com 2023-03-10 03:16 出处:网络
How to bind textbox text property, within datatemplate,to ContentControl Content property? (Without bindng via ElementName)

How to bind textbox text property, within datatemplate, to ContentControl Content property?

(Without bindng via ElementName)

This开发者_JAVA百科 is my code( that doesn't work):

<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>


Use a relative source binding:

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

Edit: I probably should note that, in terms of what the binding targets, this is equivalent to {Binding}, as the DataContext in the ContentTemplate is the Content.

However binding directly to the DataContext will not propagate back to the source DataContext, hence the Content of the ContentControl would not change when when using this binding (or the two-way compliant variation {Binding .}, which changes absolutely nothing as far as I can tell).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号