如何在 XAML 中为不同控件使用自定义样式
我已经为 ressource-dic 中的按钮编写了自定义样式。我的问题是,是否可以对不同的按钮使用这种样式?意味着我需要通过调用样式来设置参数来切换模具图片目标。 (如何?)
窗口:
<Button .... Style="{DynamicResource downloadButtonStyle}" IsEnabled="True" />
RessourceDic:
<Style x:Key="downloadButtonStyle" TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Image x:Name="PART_img" Source="/FtpUploadClient;component/media/box_48.png"/>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_img" Property="Source" Value="/FtpUploadClient;component/media/box_download_48.png" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="PART_img" Property="Source" Value="/FtpUploadClient;component/media/box_deactivated_48.png" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I have coded a custom style for a button in a ressource-dic. My question is, is it possible to use this style for different buttons? Means that I need to set params by calling the style to switch die picture target. (How?)
Window:
<Button .... Style="{DynamicResource downloadButtonStyle}" IsEnabled="True" />
RessourceDic:
<Style x:Key="downloadButtonStyle" TargetType="{x:Type Button}">
<Setter Property="IsEnabled" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Image x:Name="PART_img" Source="/FtpUploadClient;component/media/box_48.png"/>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="PART_img" Property="Source" Value="/FtpUploadClient;component/media/box_download_48.png" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="PART_img" Property="Source" Value="/FtpUploadClient;component/media/box_deactivated_48.png" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用附加属性。这将使您能够为每个
Button
实例指定Template
内的Images
源在
Template
中< em>按钮扩展
You can use Attached Properties. This will enable you to specify the source for the
Images
inside theTemplate
for eachButton
instanceIn the
Template
ButtonExtension