Update source with TemplateBinding

Polaris picture Polaris · May 12, 2010 · Viewed 7.2k times · Source

I use this style for all my labels

    <Style TargetType="Label" x:Key="LabelStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <StackPanel Orientation="Horizontal"  >
                        <TextBox Loaded="MyTextBlock_Loaded" x:Name="EditControl" Visibility="Collapsed" Text="{TemplateBinding Tag}" />
                        <Label Content="{TemplateBinding Content}" Grid.Column="1" Grid.Row="1">
                        </Label>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

and my sample label

<Label Grid.Column="0" Grid.Row="0" Content="Photo" Style="{StaticResource LabelStyle}" Tag="{Binding fieldsCode.firstName, UpdateSourceTrigger=PropertyChanged}"/>

But I feel that TemplateBiding doesn't support update of property. How can solve this issue

Answer

Amsakanna picture Amsakanna · May 12, 2010

Try this for two-way binding

Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag, Mode=TwoWay}"