Update source with TemplateBinding
Asked Answered
U

2

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

Ultramontane answered 12/5, 2010 at 10:46 Comment(0)
T
28

Try this for two-way binding

Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag, Mode=TwoWay}"
Thames answered 12/5, 2010 at 11:43 Comment(10)
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 bindingUltramontane
Veer is it possible that Tag property of Label not support data changing?Ultramontane
@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.Thames
@Veer it doesn't work. Can you check this solution in VS? Again one way bindingUltramontane
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 :(Ultramontane
@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?Ultramontane
@Polaris: I don't have VS right now. Can you post your recent version?Thames
@Polaris: Won't an UserControl suit your need?Thames
@Polaris: No! not your VS version. I asked your modified code.Thames
I cannot use UserControl because everytime my binding expresiion is different. If I use UserControl I must hardcode BindingExpressionUltramontane
K
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}" />

Keldah answered 12/5, 2010 at 11:1 Comment(1)
{Binding Tag, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}Thicken

© 2022 - 2024 — McMap. All rights reserved.