How do you color a rectangle in C# that has been declared in XAML in WPF?
There is a rectangle control in XAML. In my C# code there are times in which it would be n
Rectangle blueRectangle = new Rectangle();
// Fill rectangle with blue color
blueRectangle.Fill = blueBrush;
The way that worked for me is to have an embedded textblock and change the background to the textblock.
Rectangle Colour in XAML for Windows Phone:
<Rectangle Width="480" Height="200">
<Rectangle.Fill>
<SolidColorBrush Color="Azure" />
</Rectangle.Fill>
</Rectangle>
Set the x:Name
property in your XAML to what you want to refer to the rectangle as in your c# code. Then, you can access all of the properties of the rectangle in code, such as whateverYouNamedIt.Fill
For all the lazy programmers, the .net color enums:
myRectangle.Fill = new SolidColorBrush(System.Windows.Media.Colors.AliceBlue);
Assuming you named your rectangle myRectangle, you can color it using the Fill property:
myRectangle.Height = 200;
myRectangle.Width = 200;
myRectangle.Stroke = new SolidColorBrush(Color.FromRgb(0, 111, 0));
myRectangle.Fill = new SolidColorBrush(Color.FromRgb(0, 111, 111));