locked
Setting Fill Property at Runtime

    Question

  • How can I set the Fill property of one rectangle object at runtime? I just need to change it's fill color, for example from "Red" to "Blue". Could somebody send me one sample of code, or one place where I can find some samplesof C++/XAML in Metro Style applications?

    Thank you.

    Friday, December 23, 2011 11:13 PM

Answers

  • Hi Leo,

    Something similar to this will work:

                SolidColorBrush myBrush = new SolidColorBrush(Colors.Green);
    
                Scenario1Rectangle.Fill = myBrush;
    
    

     

    -Jeff 


    Jeff Sanders (MSFT)
    Wednesday, December 28, 2011 2:49 PM
    Moderator
  • Thanks Jeff, it worked.

     

    See how the C++ code looks:

     

     

           Windows::UI::Xaml::Media::SolidColorBrush^ myBrush = ref new Windows::UI::Xaml::Media::SolidColorBrush();

          

           myBrush->Color = Windows::UI::Xaml::Media::Colors::Green;

          

     

           this->StatusRectangle->Fill = myBrush;

     

    The namespaces can be supressed with the "using", but I kept it for other developers reference.

    Thank you.

    Monday, January 2, 2012 2:04 AM

All replies

  • Hi Leo,

    Something similar to this will work:

                SolidColorBrush myBrush = new SolidColorBrush(Colors.Green);
    
                Scenario1Rectangle.Fill = myBrush;
    
    

     

    -Jeff 


    Jeff Sanders (MSFT)
    Wednesday, December 28, 2011 2:49 PM
    Moderator
  • Thanks Jeff, it worked.

     

    See how the C++ code looks:

     

     

           Windows::UI::Xaml::Media::SolidColorBrush^ myBrush = ref new Windows::UI::Xaml::Media::SolidColorBrush();

          

           myBrush->Color = Windows::UI::Xaml::Media::Colors::Green;

          

     

           this->StatusRectangle->Fill = myBrush;

     

    The namespaces can be supressed with the "using", but I kept it for other developers reference.

    Thank you.

    Monday, January 2, 2012 2:04 AM
  • Excellent!  Thanks for sharing the C++ version!
    Jeff Sanders (MSFT)
    Tuesday, January 3, 2012 1:03 PM
    Moderator