我有这个:
<ControlTemplate TargetType="Window">
<TextBlock x:Name="tbFoo" Text="LOREM IPSUM" />
</ControlTemplate>
有没有办法tbFoo
在运行时更改文本?
选项1:
将该属性绑定到 的某个属性TemplatedParent
:
<ControlTemplate TargetType="Window">
<TextBlock x:Name="tbFoo" Text="{TemplateBinding Title}" />
</ControlTemplate>
然后:
<Window Title="My Window"/>
将导致tbFoo
具有“我的窗口”文本。
选项 2:使用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>