Change background-color

I have been asked many times, how to change the background-color of a designeritem. So here it comes, have fun.

The implementation is actually quite simple. We’ll add a new contextmenu-item and some code to change the background- and strokecolor of the designeritem.

  1. Open the file DesignerCanvas.Commands.cs
  2. Add the following at the top of the DesignerCanvas-class:
    public static RoutedCommand ChangeBackgroundColor = new RoutedCommand();
    
  3. Add the following in the constructor:
    this.CommandBindings.Add(new CommandBinding(DesignerCanvas.ChangeBackgroundColor, ChangeBackgroundColor_Executed, ChangeBackgroundColor_Enabled));
    
  4. Add the following below the constructor:
    private void ChangeBackgroundColor_Executed(object sender, ExecutedRoutedEventArgs e)
    {
    	// Get the item on which the contextmenu was raised
    	DesignerItem designerItem = e.Source as DesignerItem;
    
    	// Fill the shape blue
    	((System.Windows.Shapes.Path)designerItem.Content).Fill = new SolidColorBrush(Color.FromRgb(0, 0, 255));
                
    	// Set the border to green
    	((System.Windows.Shapes.Path)designerItem.Content).Stroke = new SolidColorBrush(Color.FromRgb(0, 255, 0));
    }
    
    private void ChangeBackgroundColor_Enabled(object sender, CanExecuteRoutedEventArgs e)
    {
    	e.CanExecute = true;
    }
    
  5. Open the file DesignerItem.xaml
  6. Above the cut-menuitem add the following:
    <MenuItem Header="Change color" Command="{x:Static s:DesignerCanvas.ChangeBackgroundColor}" />
    

If you want to have a selector for the color, google for wpf color picker ;-)