views:

203

answers:

2

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

A: 

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
+1  A: 

Try this for two-way binding

Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag, Mode=TwoWay}"
Veer
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
Veer is it possible that Tag property of Label not support data changing?
Polaris
@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.
Veer
@Veer it doesn't work. Can you check this solution in VS? Again one way binding
Polaris
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
@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
@Polaris: I don't have VS right now. Can you post your recent version?
Veer
VS 2008 Sp1 framework 3.5
Polaris
@Polaris: Won't an UserControl suit your need?
Veer
@Polaris: No! not your VS version. I asked your modified code.
Veer
I cannot use UserControl because everytime my binding expresiion is different. If I use UserControl I must hardcode BindingExpression
Polaris