【发布时间】:2011-10-18 16:32:11
【问题描述】:
我们可以使用如下代码的控件模板轻松制作图标按钮:
<Style x:Key="IconButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid>
<Image x:Name="Background" Source="/UOC;component/TOOLBAR_BUTTON_NORMAL.png"/>
<Image Source="/UOC;component/ICON_SLICER.gif" Width="20" Height="20" Margin="0,-10,0,0"/>
<TextBlock Foreground="White" FontSize="9" Text="{TemplateBinding Button.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,15,0,0"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Source" TargetName="Background" Value="/UOC;component/TOOLBAR_BUTTON_OVER.png"/>
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="Source" TargetName="Background" Value="/UOC;component/TOOLBAR_BUTTON_CLICK.png"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
但我认为这在实践中并不是一种有效的方式。因为我无法为每个图标按钮制作多种样式。 (例如,假设 App 中有三个按钮:“打开”按钮、“关闭”按钮和“导航”按钮。这些按钮具有不同的图标集。我无法制作像“IconButton_Close”、“IconButton_Open”、“IconButton_Nav”这样的样式. 这太愚蠢了。)
UserControl 可能是一个答案。但我认为这不是一个聪明的方法。因为如果我制作 UserControl,它将只是 Button 控件的包装。这不是正确的方法。
所以,给我一个图标按钮的最佳方式。
谢谢。
【问题讨论】:
标签: wpf templates button icons