c#-全局按钮的动态样式,每个按钮具有不同的图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-全局按钮的动态样式,每个按钮具有不同的图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1760字,纯文字阅读大概需要3分钟。
内容图文
![c#-全局按钮的动态样式,每个按钮具有不同的图像](/upload/InfoBanner/zyjiaocheng/655/9d386c0057a84c1f9f7925f65f19444c.jpg)
我在Wpf C#应用程序中使用Buttons.
我已经为这些按钮声明了一种样式:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Margin="0" Width="100" Height="60" >
//some more code here that sets color and stuff
<ContentPresenter HorizontalAlignment="Stretch" />
<Image Source="Images/search_48.png" / >
//some more code here (triggers)
</Setter.Value>
</Setter>
</Style>
我希望将此样式应用于“窗口”中的所有按钮,但图像有所不同.
通过复制代码并为每个按钮设置自己的样式,同时仅更改图像,我可以创建许多样式,但是我不希望这种冗余.
我将按钮样式设置如下:
<Button x:Name="buttonName"
Style="{DynamicResource ButtonStyle}"
Content="button text">
正如我提到的,我有许多这样的按钮,我希望它们都具有相同的样式,但要为每个按钮更改这种样式的图像源.
有没有一种方法可以在不为每个按钮创建具有不同名称的相同样式的情况下做到这一点?
解决方法:
您为什么不将图像放在按钮内而不是样式内.
<Button x:Name="buttonName" Style="{DynamicResource ButtonStyle}" >
<Image Source="Images/search_48.png" / >
</Button>
然后,样式将如下所示:
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Margin="0" Width="100" Height="60" >
//some more code here that sets color and stuff
<ContentPresenter HorizontalAlignment="Stretch" />
//some more code here (triggers)
</Setter.Value>
</Setter>
</Style>
更新:如果您有文本和图像,请使用堆栈面板:
<Button x:Name="buttonName" Style="{DynamicResource ButtonStyle}" >
<StackPanel Orientation="Horizontal">
<Image Source="Images/search_48.png" Stretch="None" />
<TextBlock>text</TextBlock>
</StackPanel>
</Button>
使用堆栈面板将使您可以将垂直或水平方向用于按钮布局.然后可以使用按钮内的边距定位文本和图像.
内容总结
以上是互联网集市为您收集整理的c#-全局按钮的动态样式,每个按钮具有不同的图像全部内容,希望文章能够帮你解决c#-全局按钮的动态样式,每个按钮具有不同的图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。