Reputation: 561
I was just playing around with a simple ContentPresenter
.
<TextBox Text="{Binding Path=TestString, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<ContentPresenter Content="{Binding}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Path=TestString, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
This code works perfectly - if I enter something to the Stackpanel Textbox the Textbox outside gets actualiced. The other way round works too.
If I change the code to:
<TextBox Text="{Binding Path=TestString, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<ContentPresenter Content="{Binding Path=TestString, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
When I change the Textbox outside of the ContentPresenter it gets actualiced BUT if I change the Text inside the Template it does not get converted back.
Why?
Thank you!
Upvotes: 1
Views: 639
Reputation: 12533
<ContentPresenter Content="{Binding Path=TestString,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text="{Binding Path=., UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
Binding to Path=. means Binding to the DataContext object. Inside of a ContentTemplate the DataContext is the Content. i.e. your DataContext is a String. in this case the String received from the Binding to TestString.
use snoop to observe this.
Edit :
is this what you are looking for
<TextBox Text="{Binding Path=Content,RelativeSource={RelativeSource AncestorType=ContentPresenter}}"/>
BTW: ContentPresenter by itself inside a control template does all this for you by default.
Upvotes: 1