Associating Data with ContentControl Content

How to associate the text property textbox, within the datatemplate, with the ContentControl Content property?
(No binding via ElementName)

This is my code (which does not 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> 
+3
source share
1 answer

Use relative source binding:

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

Edit: I should probably point out that in terms of binding purposes this is equivalent to {Binding} , since the DataContext in the ContentTemplate is Content .

However, the binding directly to the DataContext will not extend back to the DataContext source, so the Content ContentControl will not change when using this binding (or the two-way {Binding .} Match, which does not change anything, as far as I can tell).

+5
source

Source: https://habr.com/ru/post/912709/


All Articles