WPF在运行时更改控件模板中的文本
问题描述:
我正在制作一个模板控件,以便可以让按钮的图像在您点击时发生变化。我也试图在运行时可以更改按钮的顶部获取文本。我有按钮图像和一切工作,但我似乎无法在运行时获得该标签,所以我可以更改文本。这是xaml中的代码。我失踪背后WPF在运行时更改控件模板中的文本
<UserControl.Resources>
<ControlTemplate TargetType="{x:Type Button}" x:Key="ActionButton">
<Grid>
<Label Panel.ZIndex="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Arial" Name="lblText" Foreground="#5E4421" FontWeight="Bold" FontSize="14">Test</Label>
<Image Name="Normal" Source="/AssaultWare.Controls;component/Replayer/Images/button_off.png"/>
<Image Name="Pressed" Source="/AssaultWare.Controls;component/Replayer/Images/button_on.png"/>
<Image Name="Disabled" Source="/AssaultWare.Controls;component/Replayer/Images/button_off.png" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
...
</ControlTemplate.Triggers>
</ControlTemplate>
</UserControl.Resources>
<Button Canvas.Left="471" Canvas.Top="465" Template="{StaticResource ActionButton}" Name="btnRight"/>
答
难码破译你的问题,但我认为你只需要更改Label
到ContentControl
及其Content
属性绑定到Button
的Content
属性:
<ContentControl Content="{TemplateBinding Content}" .../>
我可能已经很难破译,但你钉了它。谢谢你,先生 – 2010-10-28 22:55:52