【发布时间】:2012-05-09 21:41:55
【问题描述】:
如何在悬停和单击时更改按钮的背景图像? Visual Studio 的 UI 似乎没有提供任何简单的方法来做到这一点。目前,默认行为似乎将我的图像替换为纯色,看起来很棒。
到目前为止,我只有 Button 基础:
<Button Content="" Height="75" VerticalAlignment="Center" Width="75" HorizontalAlignment="Center" ClickMode="Press">
<Button.Background>
<ImageBrush ImageSource="../data/images/icons/skill_icon_0.png"/>
</Button.Background>
</Button>
我尝试处理事件并手动设置它,但它不适用于 Pressed/Released:
Button skillButton = new Button();
skillButton.Width = 75;
skillButton.Height = 75;
skillButton.ClickMode = ClickMode.Press;
skillButton.Background = GetIconImage(iconIndex, 0);
skillButton.PointerEntered +=
(object sender, Windows.UI.Xaml.Input.PointerEventArgs e) => {
skillButton.Background = GetIconImage(iconIndex, 1);
};
skillButton.PointerExited +=
(object sender, Windows.UI.Xaml.Input.PointerEventArgs e) => {
skillButton.Background = GetIconImage(iconIndex, 0);
};
skillButton.PointerPressed +=
(object sender, Windows.UI.Xaml.Input.PointerEventArgs e) => {
skillButton.Background = GetIconImage(iconIndex, 2);
};
skillButton.PointerReleased +=
(object sender, Windows.UI.Xaml.Input.PointerEventArgs e) => {
if (skillButton.FocusState == FocusState.Pointer)
skillButton.Background = GetIconImage(iconIndex, 1);
else skillButton.Background = GetIconImage(iconIndex, 0);
};
【问题讨论】:
标签: image xaml button windows-8 windows-runtime