WPF 在运行时更改控件模板中的文本

发布于 2024-09-30 04:31:35 字数 1144 浏览 7 评论 0原文

我正在制作一个模板控件,这样我就可以有一个带有图像的按钮,当您单击它时,该图像会发生变化。我还试图在按钮顶部获取可以在运行时更改的文本。我有按钮图像和一切工作,但我似乎无法在运行时获得该标签,因此我可以更改文本。这是 xaml 中的代码。我缺少后面的代码

<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"/>

I am making a template control so that I can have a button with an image that changes when you click it. I also am trying to get text on top of the button that can change at run time. I have the button images and everything working but I can't seem to get that label at runtime so I can change the text. Here is the code in the xaml. I am missing the code behind

<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"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-10-07 04:31:35

很难破译您的问题,但我认为您只需将 Label 更改为 ContentControl 并将其 Content 属性绑定到 按钮Content属性:

<ContentControl Content="{TemplateBinding Content}" .../>

Difficult to decipher your question, but I think you just need to change the Label to a ContentControl and bind its Content property to the Button's Content property:

<ContentControl Content="{TemplateBinding Content}" .../>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文