使用按钮模板在按钮中文本框的动态属性
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在这些页面上找到了您的答案:
您可以简单地将绑定到ControlTemplate上的文本block的文本属性。
您应该将按钮的 content 属性添加到TextBlock的文本属性中:
然后,您可以设置按钮的内容属性,按钮的 content 属性将传递给您的< em> textblock 's text 属性。
我用这些按钮进行了测试:
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:
Then you can set the content property of Button, the Content property of Button will be passing to the your TextBlock's Text property.
I tested with these Buttons: