在wpf中给button设置背景图片,点击button会不停的闪烁.
button是我动态创建的。foreach (var WebHttp in WebHttpList)
{
Button btn = new Button();
string BtnName = "newbtn" + WebHttp.WebHttpId.ToString();
btn.Name = BtnName;
btn.Width = 100;
btn.Height = 50;
Thickness th = new Thickness(10, 10, 10, 10);
btn.Margin = th;
btn.HorizontalAlignment = HorizontalAlignment.Left;
btn.VerticalAlignment = VerticalAlignment.Top;
ImageBrush brush1 = new ImageBrush();
string path = System.Windows.Application.Current.StartupUri.OriginalString.Replace("login.xaml", "");
brush1.ImageSource = new BitmapImage(new Uri(path + WebHttp.WebHttpText, UriKind.Absolute));
btn.Background = brush1; //在这里添加按钮图片
btn.Tag = WebHttp.WebHttpName;
btn.Style = Resources["btnStyle"] as Style;
btn.Content = WebHttp.WebHttpName;
btn.AddHandler(Rectangle.MouseDownEvent, new RoutedEventHandler(OnButton));
btn.MouseMove += new MouseEventHandler(this.MoveButton);
btn.MouseLeave += new MouseEventHandler(this.MouseLeaveButton);
wrapPanels.Children.Add(btn);
wrapPanels.RegisterName(BtnName, btn);
} --------------------编程问答-------------------- http://archive.cnblogs.com/a/2071993/
建议用时间控件试试 --------------------编程问答-------------------- 别用背景图片了,把按钮的Content设置成图片
我也刚刚遇到这个问题
ImageBrush imgBrush = new ImageBrush();
imgBrush.ImageSource = new BitmapImage(new Uri(imgPath, UriKind.Relative));
imgBrush.Stretch = Stretch.Fill;
Image img = new Image();
img.Source = imgBrush.ImageSource;
btn.Content = img;
--------------------编程问答--------------------
楼主问题解决了吗?我这样试过了,没问题的。
补充:.NET技术 , C#