How do you color a rectangle in C# that has been declared in XAML in WPF? How do you color a rectangle in C# that has been declared in XAML in WPF? wpf wpf

How do you color a rectangle in C# that has been declared in XAML in WPF?


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));


Rectangle Colour in XAML for Windows Phone:

<Rectangle Width="480" Height="200">    <Rectangle.Fill>        <SolidColorBrush Color="Azure" />    </Rectangle.Fill></Rectangle>