9

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

Polaris
  • 3,643
  • 10
  • 50
  • 61

2 Answers2

28

Try this for two-way binding

Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag, Mode=TwoWay}"
Amsakanna
  • 12,254
  • 8
  • 46
  • 58
  • Veer it works like one way binding, Must I use UpdateSourceTrigger in TextBox or in Label? I implement INotifierPropertyChanged in my class which property I use in Label binding – Polaris May 13 '10 at 05:52
  • Veer is it possible that Tag property of Label not support data changing? – Polaris May 13 '10 at 06:32
  • @Polaris: Just now saw the UpdateSourceTrigger in your Label's Tag, which is not required. If at all required it should be in the Target ie, Textbox, since it is used to propagate the target's changes to the source. By default in two way binding(check my edit), the target's(textbox) changes will be propagated to the source when it loses focus. If you really want the changes to be propagated on textchange, then you should add the updatesourcetrigger to the textbox instead. – Amsakanna May 13 '10 at 08:26
  • @Veer it doesn't work. Can you check this solution in VS? Again one way binding – Polaris May 13 '10 at 10:04
  • Is it possible to send BindingExpression to ControlTemplate?. If I bind my textblock directly to my class property everything will be work fine, but I bind to Tag property :( – Polaris May 13 '10 at 10:07
  • @Veer I mean something like this Text="{Binding Path={TemplateBinding Tag}, Mode=TwoWay}" and in the Tag Property use: Tag = "firstName" I cannot find analogic construction in inet. How can I send BindingExpression like parametr? – Polaris May 13 '10 at 10:21
  • @Polaris: I don't have VS right now. Can you post your recent version? – Amsakanna May 13 '10 at 10:43
  • @Polaris: Won't an UserControl suit your need? – Amsakanna May 13 '10 at 10:53
  • @Polaris: No! not your VS version. I asked your modified code. – Amsakanna May 13 '10 at 11:00
  • I cannot use UserControl because everytime my binding expresiion is different. If I use UserControl I must hardcode BindingExpression – Polaris May 13 '10 at 11:35
1

If you want a one-way binding from within the ControlTemplate to a property of its templated parent, use {TemplateBinding}. For all other scenarios use {Binding} instead:

<TextBox Loaded="MyTextBlock_Loaded" x:Name="EditControl" Visibility="Collapsed" Text="{Binding Tag, Mode=TwoWay}" />

bitbonk
  • 48,890
  • 37
  • 186
  • 278