0

我有这个:

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

有没有办法tbFoo在运行时更改文本?

4

1 回答 1

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>
于 2013-07-25T19:30:05.780 回答