【发布时间】:2018-03-07 16:46:55
【问题描述】:
在 XAML UWP 应用程序中,我有一个扩展 Button 的类。 我已经设置了背景 ImageBrush。
我的问题是,当我的按钮获得焦点或鼠标悬停事件时,我的按钮上会出现一个带有黑色边框的灰色矩形。
我已经尝试了 一大堆 解决方案,从更改前景到在各种事件(gotFocus、mouseEntered、mouseover)上修改 FocusVisualPrimary/SecondaryBrush。
没有任何效果,我得到的最好结果是在mouseover 事件上设置button.Background = "originalBitmapImage"(我创建了一个与原始背景具有相同图像路径的新ImageBrush,然后将其归因于BackGround),但是当鼠标悬停时图像全部闪烁触发(因为它每次都重新加载一个新图像)。
这是显示问题的图片(左:普通按钮,右:鼠标悬停的相同按钮):
我想在这两种情况下保持相同的图像,我有点不知所措。
public class MyButton : Button
{
private static string Image_path = "ms-appx:///assets/Button.png";
public MyButton()
{
ImageBrush brush = new ImageBrush()
{
ImageSource = new BitmapImage(new Uri(MyButton.Image_path))
};
this.Background = brush;
this.PointerEntered += a;
}
// This almost work, but the image is flickering when event is fired
private void a(object sender, PointerRoutedEventArgs e)
{
ImageBrush brush = new ImageBrush()
{
ImageSource = new BitmapImage(new Uri(MyButton.Image_path))
};
//this.Foreground = brush;
this.Background = brush;
}
}
【问题讨论】:
-
如果您只想停止闪烁,请将这些图像保存在一些外部字段中。如果你想防止这种奇怪的行为,你可以创建模板,或者更容易使用 ContentControl 并将图像设置为内容,或者使用它的背景属性设置边框