0

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?

Elmo
  • 6,409
  • 16
  • 72
  • 140

1 Answers1

2

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>
Community
  • 1
  • 1
Federico Berasategui
  • 43,562
  • 11
  • 100
  • 154