views:

200

answers:

1

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>

+1  A: 

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

HTH, Kent

Kent Boogaart
Thanks, Kent. I'm seeking two different binding behaviors. I'd like the UpdateSourceTrigger for the TextBlock to be PropertyChanged and for the Foo object I'd like it to be LostFocus. I should have mentioned that in my original post.
MadPokey