MadPokey
MadPokey

Reputation: 87

Binding to a data template control property

Is it possible to bind something to a property of a control in a data template entirely in XAML? The following code is a simplified version of the problem I'm running into. I'd like the text of the TextBlock (displayName) to be updated as the user types in the TextBox located in the DataTemplate.

<Window x:Class="WpfApplication4.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication4="clr-namespace:WpfApplication4"
Title="Window1" Height="300" Width="300">
<Window.Resources>
    <DataTemplate DataType="{x:Type WpfApplication4:Foo}">
        <TextBox Text="{Binding Path=Name}" />
    </DataTemplate>
    <WpfApplication4:Foo x:Key="testObject" Name="This is a test" />
</Window.Resources>
<StackPanel>
    <TextBlock x:Name="displayName" Margin="5" />
    <ContentControl x:Name="contentControl" Margin="5" Content="{StaticResource testObject}" />
</StackPanel>

Upvotes: 2

Views: 720

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178630

No, at least, not from XAML. You could write code to traverse the visual tree and find the element you want to bind to, but that would be nasty.

But in your particular example, would it not make sense to just bind the TextBlock to the same data object (Foo instance)?

Upvotes: 1

Related Questions