使用按钮模板在按钮中文本框的动态属性

发布于 2025-01-18 22:41:36 字数 1761 浏览 4 评论 0原文

我在C#WPF中可以设置一些按钮,我喜欢使用按钮模板。

我可以设置模板然后在网格中使用它,但是如何更改每个按钮的文本框的文本以及其他属性(例如矩形的颜色)?

这是我的模板:

<Window.Resources>
 <ControlTemplate x:Key="menuButton_Type1" TargetType="Button">
  <Grid >
   <Rectangle x:Name="Normal" Fill="#FFFDC776" HorizontalAlignment="Left" Height="25" Width="82" RadiusX="7" RadiusY="7"/>
   <Rectangle x:Name="Pressed" Fill="White" HorizontalAlignment="Left" Height="25" Width="82" RadiusX="7" RadiusY="7" Visibility="Hidden"/>
   <Rectangle x:Name="Disable" Fill="#FF707070" HorizontalAlignment="Left" Height="25" Width="82" RadiusX="7" RadiusY="7" Visibility="Hidden"/>
   <Border Width="82" Height="25" Padding="0,0,5,0">
    <TextBlock Text="EXIT" FontFamily="{StaticResource Swiss911}" FontSize="18" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
   </Border>
  </Grid>
  <ControlTemplate.Triggers>
   <Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="Normal" Property="Visibility" Value="Hidden" />
    <Setter TargetName="Pressed" Property="Visibility" Value="Visible" />
   </Trigger>
   <Trigger Property="IsEnabled" Value="False">
    <Setter TargetName="Normal" Property="Visibility" Value="Hidden" />
    <Setter TargetName="Disable" Property="Visibility" Value="Visible" />
   </Trigger>
  </ControlTemplate.Triggers>
 </ControlTemplate>
</Window.Resources>

我使用XAML网格中的“模板”按钮:

<Grid>
 <Button Template="{StaticResource menuButton_Type1}" Margin="18,226,-18,-226" />
</Grid>

我想更改当前设置为“退出”的 textblock 的文本,以及其中一个矩形的颜色,在这种情况下,正常。我该怎么做?

我试图使用动态属性,但这对我不起作用。

I have a few buttons to set up in C# WPF, and I like to use a button template.

I can set the template and then use it in my grid, but how do I change the text of the TextBox per button and also other properties like the color of the rectangle?

Here is my template:

<Window.Resources>
 <ControlTemplate x:Key="menuButton_Type1" TargetType="Button">
  <Grid >
   <Rectangle x:Name="Normal" Fill="#FFFDC776" HorizontalAlignment="Left" Height="25" Width="82" RadiusX="7" RadiusY="7"/>
   <Rectangle x:Name="Pressed" Fill="White" HorizontalAlignment="Left" Height="25" Width="82" RadiusX="7" RadiusY="7" Visibility="Hidden"/>
   <Rectangle x:Name="Disable" Fill="#FF707070" HorizontalAlignment="Left" Height="25" Width="82" RadiusX="7" RadiusY="7" Visibility="Hidden"/>
   <Border Width="82" Height="25" Padding="0,0,5,0">
    <TextBlock Text="EXIT" FontFamily="{StaticResource Swiss911}" FontSize="18" HorizontalAlignment="Right" VerticalAlignment="Bottom" />
   </Border>
  </Grid>
  <ControlTemplate.Triggers>
   <Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="Normal" Property="Visibility" Value="Hidden" />
    <Setter TargetName="Pressed" Property="Visibility" Value="Visible" />
   </Trigger>
   <Trigger Property="IsEnabled" Value="False">
    <Setter TargetName="Normal" Property="Visibility" Value="Hidden" />
    <Setter TargetName="Disable" Property="Visibility" Value="Visible" />
   </Trigger>
  </ControlTemplate.Triggers>
 </ControlTemplate>
</Window.Resources>

I use the template button in my XAML grid like this:

<Grid>
 <Button Template="{StaticResource menuButton_Type1}" Margin="18,226,-18,-226" />
</Grid>

I would like to change the text of the TextBlock currently set to "EXIT" and also the color of one of the Rectangle, in this case the normal one. How do I do this?

I tried to use dynamic properties, but that did not work for me.

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

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

发布评论

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

评论(1

完美的未来在梦里 2025-01-25 22:41:37

我在这些页面上找到了您的答案:

您可以简单地将绑定到ControlTemplate上的文本block的文本属性。

您应该将按钮的 content 属性添加到TextBlock的文本属性中:

     <TextBlock Text="{TemplateBinding Content}" FontFamily="{StaticResource Swiss911}" FontSize="18" HorizontalAlignment="Right" VerticalAlignment="Bottom" />

然后,您可以设置按钮的内容属性,按钮的 content 属性将传递给您的< em> textblock 's text 属性。

     <Button Template="{StaticResource menuButton_Type1}" Content="Exit"  Margin="100" />

我用这些按钮进行了测试

    <Button Template="{StaticResource menuButton_Type1}" Content="Exit"  Margin="0,0,0,0" />
    <Button Template="{StaticResource menuButton_Type1}" Content="Close"  Margin="100,0,0,0" />
    <Button Template="{StaticResource menuButton_Type1}" Content="Open"  Margin="200,0,0,0" />

I found your answer on these pages:

You can simply put the binding to the Text property of TextBlock on your ControlTemplate.

You should add the Content property of the button to TextBlock's text property:

     <TextBlock Text="{TemplateBinding Content}" FontFamily="{StaticResource Swiss911}" FontSize="18" HorizontalAlignment="Right" VerticalAlignment="Bottom" />

Then you can set the content property of Button, the Content property of Button will be passing to the your TextBlock's Text property.

     <Button Template="{StaticResource menuButton_Type1}" Content="Exit"  Margin="100" />

I tested with these Buttons:

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