我还是 WPF 的新手(用它编写我的第一个应用程序),但基本上我想要(或认为我想要)创建一个具有 3 个图像(空闲、悬停、onClick)但我可以更改图像的控件. 所以现在我有:
<Rectangle Height="29" Width="35" Margin="0,2,0,0"
HorizontalAlignment="Left" VerticalAlignment="Top"
Name="BTN_OpenDeviceSettings" ToolTip="Open Device Settings"
Fill="{DynamicResource device_grid___open_grid}"
MouseEnter="BTN_OpenDeviceSettings_MouseEnter"
MouseLeave="BTN_OpenDeviceSettings_MouseLeave"
MouseLeftButtonDown="BTN_OpenDeviceSettings_MouseLeftButtonDown"
MouseLeftButtonUp="BTN_OpenDeviceSettings_MouseLeftButtonUp">
</Rectangle>
而且效果很好。但我想将图形与代码分开,为了完成这项工作,我在我的 C# 代码中手动交换了填充图像。我见过这样的代码:
<Button Name="btnNext" Padding="15,3" HorizontalAlignment="Right" Click="OnButtonClick">
<Image Width="90" Height="90">
<Image.Style>
<Style TargetType="Image">
<Setter Property="Source" Value="/resources/a.png" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="/resources/b.png" />
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</Button>
所以我认为这就是我想要的。但理想情况下,我将拥有许多这样的按钮,所以我可以创建一些东西以便我这样做:
<MyButton>
<Images idleImage="someimage.png" hoverImage="someOtherImage.png" clickImage="someOtherOtherImage.png" onCLick="some_cSharp_method_to_call" />
</MyButton>