我有这个:
<ControlTemplate TargetType="Window">
<TextBlock x:Name="tbFoo" Text="LOREM IPSUM" />
</ControlTemplate>
有没有办法在运行时更改tbFoo
的文本?
答案 0 :(得分:2)
选项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>