private static void OnCommandChanged(DependencyObject a_dependencyObject, DependencyPropertyChangedEventArgs a_e) { MenuItem menuItem = a_dependencyObject as MenuItem; if (menuItem == null) { throw new InvalidOperationException(@"MenuItem required"); } ICommand oldCommand = a_e.OldValue as ICommand; if (oldCommand != null) { menuItem.Command = null; menuItem.Icon = null; CommandToolTipHelper.ApplyCommandToolTip(menuItem, null); ICommandDescriptionProvider oldDescProvider = a_e.OldValue as ICommandDescriptionProvider; if (GetOverrideHeader(menuItem) && oldDescProvider != null) { BindingOperations.ClearBinding( menuItem, MenuItem.HeaderProperty); } } ICommand newCommand = a_e.NewValue as ICommand; if (newCommand != null) { menuItem.Command = newCommand; Uri imageUri = CommandImageHelper.GetCommandImageUri(newCommand); if (imageUri != null) { MenuItemImage image = new MenuItemImage { Source = new BitmapImage(imageUri) }; menuItem.Icon = image; } // Header ICommandDescriptionProvider newDescProvider = a_e.NewValue as ICommandDescriptionProvider; if (GetOverrideHeader(menuItem) && newDescProvider != null) { BindingOperations.SetBinding( menuItem, MenuItem.HeaderProperty, new Binding("Text") { Source = newDescProvider.Description }); } // tooltip CommandToolTipHelper.ApplyCommandToolTip(menuItem, newCommand); } }
private static void OnCommandChanged(DependencyObject a_dependencyObject, DependencyPropertyChangedEventArgs a_e) { Button button = a_dependencyObject as Button; if (button == null) { throw new InvalidOperationException(@"Button required"); } ICommand oldCommand = a_e.OldValue as ICommand; if (oldCommand != null) { button.Command = null; CommandToolTipHelper.ApplyCommandToolTip(button, null); ICommandDescriptionProvider oldDescProvider = a_e.OldValue as ICommandDescriptionProvider; if (GetOverrideContent(button) && oldDescProvider != null) { button.Content = null; } } ICommand newCommand = a_e.NewValue as ICommand; if (newCommand != null) { button.Command = newCommand; CommandToolTipHelper.ApplyCommandToolTip(button, newCommand); ICommandDescriptionProvider newDescProvider = a_e.NewValue as ICommandDescriptionProvider; if (GetOverrideContent(button) && newDescProvider != null) { var textContent = new TextBlock() { TextAlignment = TextAlignment.Center, TextWrapping = TextWrapping.Wrap, }; BindingOperations.SetBinding( textContent, TextBlock.TextProperty, new Binding("Text") { Source = newDescProvider.Description }); Uri imageUri = CommandImageHelper.GetCommandImageUri(newCommand); if (imageUri != null) { Dock?useText = GetAppendText(button); ButtonImage image = new ButtonImage() { VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, Source = new BitmapImage(imageUri), }; if (useText.HasValue) { Label text = new Label() { Margin = new Thickness(2), VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, Content = textContent, }; DockPanel.SetDock(text, useText.Value); DockPanel container = new DockPanel(); container.LastChildFill = true; container.Children.Add(text); container.Children.Add(image); button.Content = container; } else { button.Content = image; } } else { button.Content = textContent; } } } }