protected MenuItem AddMenuItem(MenuItem menu, string menuName, ICommand command, string iconName) { TextBlock txtMenuItem = new TextBlock(); txtMenuItem.Text = menuName; txtMenuItem.HorizontalAlignment = HorizontalAlignment.Left; MenuItem subMenu = new MenuItem(); subMenu.Margin = new Thickness(5, 0, 0, 0); subMenu.Command = command; subMenu.Header = txtMenuItem; if (!String.IsNullOrEmpty(iconName)) { try { EnableImage img = new EnableImage(); img.Margin = new Thickness(-5, 0, 5, 0); img.Width = 16; img.Height = 16; img.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("pack://application:,,,/CFCObjects;component/Images/MenuImg/" + iconName, UriKind.Absolute)); subMenu.Icon = img; } catch (Exception) { } } // 将菜单添加到ContextMenu中 if (menu != null) { menu.Items.Add(subMenu); } else { if (this.ContextMenu == null) { this.ContextMenu = new ContextMenu(); } this.ContextMenu.Items.Add(subMenu); } return(subMenu); }
void ImageButton_Loaded(object sender, RoutedEventArgs e) { StackPanel sp = new StackPanel(); sp.Orientation = Orientation.Horizontal; ImageSource imgSource = null; try { if (MyImageSource != null && MyImageSource is ImageSource) { imgSource = (ImageSource)MyImageSource; } else if (MyImageSource != null) { imgSource = new BitmapImage(new Uri(MyImageSource.ToString(), UriKind.RelativeOrAbsolute)); } _img = new EnableImage() { Source = imgSource, Width = 16 }; sp.Children.Add(_img); } catch { } try { sp.Children.Add(new TextBlock() { Text = MyText, Margin = new Thickness(5, 0, 0, 0), HorizontalAlignment = System.Windows.HorizontalAlignment.Center }); } catch { } this.Content = sp; }