我创建了一个按钮模板。有一个图像和一个标签。图像的来源在 ResourceDictionary Pictures 中。图片的 Source 是一个带有白色画笔颜色的 DrawingImage。
<DrawingImage x:Key="Moduly">
<DrawingImage.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="{Binding BackgroundColor}" Geometry="M 0,0L 32,0L 32,32L 0,32L 0,0 Z " />
<GeometryDrawing Brush="{Binding IconColor}" Geometry="M800.336 949.171H184.527c-80.603 0-136.768-65.56-136.768-146.159V203.677 c0-80.602 56.166-152.42 136.768-152.42h615.809c80.598 0 149.289 71.818 149.289 152.42v599.334 C949.625 883.611 880.934 949.171 800.336 949.171L800.336 949.171z M874.488 203.677c0-39.184-34.99-77.287-74.152-77.287H184.527 c-39.166 0-61.632 38.103-61.632 77.287v599.334c0 39.182 22.466 71.024 61.632 71.024h615.809 c39.162 0 74.152-31.843 74.152-71.024V203.677L874.488 203.677z M273.604 613.832h447.829v75.134H273.604V613.832L273.604 613.832z M273.604 463.566h447.829v75.131H273.604V463.566L273.604 463.566z M273.604 313.295h447.829v75.134H273.604V313.295 L273.604 313.295z" />
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
所有样式(Button、StackPanel、Label)的来源都在资源字典 Styles 中。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Button">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Background" Value="#103255"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Resources>
<Style TargetType="StackPanel">
<Style.Resources>
<Style TargetType="Label">
<Setter Property="Foreground" Value="White" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsPressed, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"
Value="True">
<Setter Property="Foreground" Value="#103255" />
</DataTrigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
</Style.Resources>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#103255"/>
<Setter Property="BorderBrush" Value="Black"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
按下按钮时,我需要更改 DrawingImage 的画笔颜色。只有按下按钮内的特定一张图片。
<Button Grid.Column="0" Grid.Row="2" Name="modulyButton"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
BorderThickness="3" Margin="0,0,0,0">
<StackPanel Orientation="Vertical" Name="modulyStackPanel"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Image x:Name="modulyImage" Source="{StaticResource Moduly}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Height="30" Width="30"/>
<Label x:Name="modulyLabel" Content="MODULY"
VerticalAlignment="Center"/>
</StackPanel>
</Button>
有没有人有办法解决吗?