none
Change Canvas Background Colour Dynamically

    Question

  • I'm trying to change the background colour of my canvas dynamically using this code:

     

    private void colourComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          int colour = colourComboBox.SelectedIndex;
          switch (colour)
          {
            case 0: canvas.Background = new SolidColorBrush(Colors.Black);
              break;
    
            case 1: canvas.Background = new SolidColorBrush(Colors.White);
              break;
    
            case 2: canvas.Background = new SolidColorBrush(Colors.Gray);
              break;
    
            case 3: canvas.Background = new SolidColorBrush(Colors.Transparent);
              break;
    
            default: canvas.Background = new SolidColorBrush(Colors.Black);
              break;
          }
        }
    
    However, when I run my program, I get this exception:

     

    A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

     

    Any ideas?

    Monday, April 04, 2011 8:25 PM

Answers

  • hi,

     

    To change canvas background color

    canvas1.Background = new SolidColorBrush(Colors.Blue);

    which is same as you used, you are getting exception that might occur because of issues in your XAML file. If you can upload your application then i can have look into it if you wish.


    Vipul Mistry
    • Marked as answer by colinlim Wednesday, April 20, 2011 3:50 PM
    Monday, April 04, 2011 8:51 PM

All replies

  • hi,

     

    To change canvas background color

    canvas1.Background = new SolidColorBrush(Colors.Blue);

    which is same as you used, you are getting exception that might occur because of issues in your XAML file. If you can upload your application then i can have look into it if you wish.


    Vipul Mistry
    • Marked as answer by colinlim Wednesday, April 20, 2011 3:50 PM
    Monday, April 04, 2011 8:51 PM
  • Hi Colinlim,

    Could you post more details about the exception? It will be very helpful to know call stack and the inner exception as well (if any).

    You can click the Copy Details to Clipboard button at the bottom of the exception dialog and paste the information here.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 06, 2011 2:20 AM
    Moderator