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

后端 未结 8 2098
长情又很酷
长情又很酷 2020-12-20 11:26

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

相关标签:
8条回答
  • 2020-12-20 11:36
    Rectangle blueRectangle = new Rectangle();
    // Fill rectangle with blue color
    blueRectangle.Fill = blueBrush;
    
    0 讨论(0)
  • 2020-12-20 11:37

    The way that worked for me is to have an embedded textblock and change the background to the textblock.

    0 讨论(0)
  • 2020-12-20 11:40

    Rectangle Colour in XAML for Windows Phone:

    <Rectangle Width="480" Height="200">
        <Rectangle.Fill>
            <SolidColorBrush Color="Azure" />
        </Rectangle.Fill>
    </Rectangle>
    
    0 讨论(0)
  • 2020-12-20 11:42

    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

    0 讨论(0)
  • 2020-12-20 11:50

    For all the lazy programmers, the .net color enums:

    myRectangle.Fill = new SolidColorBrush(System.Windows.Media.Colors.AliceBlue); 
    
    0 讨论(0)
  • 2020-12-20 11:50

    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));
    
    0 讨论(0)
提交回复
热议问题