Elmo
Elmo

Reputation: 6471

Change XAML ControlTemplate Child Elements Attributes during Runtime

I have this:

<ControlTemplate TargetType="Window">
    <TextBlock x:Name="tbFoo" Text="LOREM IPSUM" />
</ControlTemplate>

Is there a way to change the text of tbFoo during runtime?

Upvotes: 0

Views: 484

Answers (1)

Fede
Fede

Reputation: 44038

Option 1:

Bind the property to some property of the TemplatedParent:

<ControlTemplate TargetType="Window">
    <TextBlock x:Name="tbFoo" Text="{TemplateBinding Title}" />
</ControlTemplate>

Then:

<Window Title="My Window"/>

will cause the tbFoo to have the "My Window" text.

Option 2: Use Triggers:

<ControlTemplate TargetType="Window">
    <TextBlock x:Name="tbFoo"/>

    <ControlTemplate.Triggers>
       <Trigger Property="IsActive" Value="True">
          <Setter TargetName="tbFoo" Property="Text" Value="Window is Active!"/>
       </Trigger>
       <Trigger Property="IsActive" Value="False">
          <Setter TargetName="tbFoo" Property="Text" Value="Window is Inactive!"/>
       </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

Upvotes: 2

Related Questions