Armen Mkrtchyan
Armen Mkrtchyan

Reputation: 921

Bind value from on controltemplate to another

In my xaml file i have two controls: TextBox and Button. That controls have their template defined. Now i want to bind second control's template's TextBlock text value to fisr control's template's TextBlock text value.

How can i do that???

<TextBox x:Name="lll">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBlock Text="CLICK!!!!"></TextBlock>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TextBox.Style>
        </TextBox>
        <Button Content="" Margin="25,40,-25,-40">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <TextBlock Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBlock}}}"></TextBlock>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Button.Style>
        </Button>

Upvotes: 0

Views: 452

Answers (1)

Bizhan
Bizhan

Reputation: 17085

This code uses Tag of object lll to store the text:

<TextBox x:Name="lll">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox Text="{Binding ElementName=lll , Path=Tag, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </TextBox.Style>
</TextBox>
<Button Content="" Margin="25,40,-25,-40">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBlock Text="{Binding ElementName=lll, Mode=OneWay, Path=Tag}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

Upvotes: 1

Related Questions