需求描述:
给MenuItem内部的子Image设置默认图标(鼠标leave)、鼠标hover图标、和选中时的图标。
注:是给Menu内个别MenuItem修改,并且是弹出子菜单。
问题描述:
1)前提:Image绑定数据源成功,且Image设置默认图标(鼠标leave)、鼠标hover图标,已经在Image的对应事件中,通过image.source设置成功;
2)思路:在点击弹出的子菜单项时,通过修改image绑定的数据源,来设置新选中图标。
3)问题:修改image绑定的数据源成功,但图标依然显示的是默认图标。
4)分析:
a、注销掉在Image设置默认图标(鼠标leave)、鼠标hover图标的代码,则点击弹出的子菜单项时,设置新选中图标成功。
b、释放注释,跟踪点击弹出的子菜单项时,image.source的值一直不是选中的新图标。
c、释放注释,仅注释掉给image.source的相关代码,点击弹出的子菜单项时,设置新选中图标成功。跟踪image.source的值是选中的新图标。
5)结论:虽然修改image绑定的数据源成功,若存在给image.source 赋值操作,数据源并未起作用。
解决方法:
1、点击弹出的子菜单项时,将image绑定的数据源设置为选中的图标;
2、Image设置默认图标(鼠标leave)、鼠标hover图标时,不修改选中时显示的图标,仅在取消选中操作时,修改显示的图标;
3、点击弹出的子菜单项时,不仅将image绑定的数据源设置为选中的图标,而且获取该Menuitem的Image,将image.source的值是选中的新图标。
注:
操作的主体是数据源,其次才是控件,正常情况下控件的显示是随数据源变化。
参考代码如下:
1、设置Menuitem内Image的图片
/// <summary> /// 设置菜单项内的图片(因为Image.source赋值后,显示不出绑定的图片,所以又重新赋值) /// </summary> /// <param name="mi"></param> /// <param name="isFlag"></param> /// <returns></returns> private bool SetMenuitemImage(System.Windows.Controls.MenuItem mi,bool isFlag) { bool isSuccess= false; try { if (mi != null) { XmlElement xe = mi.Header as XmlElement; string imgPath0 = xe.Attributes["ImagePath0"].Value;//front + List<Image> images = this.GetChildObjects<Image>(mi); if (!isFlag) { xe.Attributes["ImagePath"].Value = imgPath0.Replace("2", string.Empty); xe.Attributes["ImagePath0"].Value = imgPath0.Replace("2", "0"); images[0].Source = new BitmapImage(new System.Uri(@"pack://application:,,," + xe.Attributes["ImagePath0"].Value)); } else { xe.Attributes["ImagePath"].Value = imgPath0.Replace("0", "2"); xe.Attributes["ImagePath0"].Value = imgPath0.Replace("0", "2"); images[0].Source = new BitmapImage(new System.Uri(@"pack://application:,,," + xe.Attributes["ImagePath0"].Value)); } } } catch (Exception ex) { isSuccess = false; } return isSuccess; }
2、Image设置默认图标(鼠标leave)、鼠标hover图标时,不修改选中时显示的图标
private void MenuItemImage_MouseEnter(object sender, EventArgs e) { Image img = sender as Image; if (img == null && sender is StackPanel)//System.Windows.Controls.MenuItem)//StackPanel// { //System.Windows.Controls.MenuItem mi = sender as System.Windows.Controls.MenuItem; //StackPanel sp = mi.DataContext as StackPanel; StackPanel sp = sender as StackPanel; img = sp.Children[0] as Image; Thread.Sleep(10); } _logger.Info(sender.GetType().ToString()); if (img != null && string.IsNullOrEmpty(_srcImgPath)) { //_srcImgPath = img.Source.ToString(); //_logger.Info(_srcImgPath); string srcPath = img.Source.ToString(); string newPath = srcPath.Replace("0", string.Empty); if (!srcPath.Contains("2")) { img.Source = new BitmapImage(new System.Uri(newPath)); } } _imgFlag = true; //img.Source = new BitmapImage(new System.Uri("pack://application:,,," + _selectedXelmt.Attributes["ImagePath"].Value));// "{Binding XPath=@ImagePath0}"; //BaseUri pack://application:,,,/DrawTool;component/startwindow.xaml Unknown System.Windows.Media.Imaging.BitmapFrameDecode } private void MenuItemImage_MouseLeave(object sender, EventArgs e) { Image img = sender as Image; if (img == null && sender is StackPanel)//System.Windows.Controls.MenuItem) { //System.Windows.Controls.MenuItem mi = sender as System.Windows.Controls.MenuItem; //StackPanel sp = mi.DataContext as StackPanel; StackPanel sp = sender as StackPanel; img = sp.Children[0] as Image; Thread.Sleep(10); } if (img != null )//&& !string.IsNullOrEmpty(_srcImgPath) { string srcPath = img.Source.ToString(); if (!srcPath.Contains("0") && !srcPath.Contains("1") && !srcPath.Contains("2")) { int dotIndex = srcPath.LastIndexOf('.'); string imgExt = srcPath.Substring(dotIndex); string newPath = srcPath.Substring(0, srcPath.Length - imgExt.Length) + "0" + imgExt; img.Source = new BitmapImage(new System.Uri(newPath)); } //img.Source = new BitmapImage(new System.Uri(_srcImgPath)); _srcImgPath = string.Empty; //_logger.Info(_srcImgPath); } //Thread.Sleep(10); _imgFlag = false; _logger.Info(sender.GetType().ToString()); //img.Source = new BitmapImage(new System.Uri("pack://application:,,," + _selectedXelmt.Attributes["ImagePath"].Value));// "{Binding XPath=@ImagePath0}"; //BaseUri pack://application:,,,/DrawTool;component/startwindow.xaml Unknown System.Windows.Media.Imaging.BitmapFrameDecode }