wpf种的各种形状的Fill属性的声明及使用

时间:2021-08-31 12:05:38

功能:获取或设置指定形状内部绘制方式的Brush.
命名空间:System.Windows.Shapes
它对应的C#语法为
public Brush Fill {get;set;}
C++为

public:
property Brush^ Fill {
Brush^ get ();
void set (Brush^ value);

此示例演示如何使用 Fill 属性来设置 Ellipse 元素的背景颜色。

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<StackPanel>
<Ellipse Fill="Red" Width="100" Height="100"/>
</StackPanel>
</Page>

对应的C#代码

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
public partial class SetBackgroundColorOfShapeExample : Page
{
public SetBackgroundColorOfShapeExample()
{
// Create a StackPanel to contain the shape.
StackPanel myStackPanel = new StackPanel();

// Create a red Ellipse.
Ellipse myEllipse = new Ellipse();

// Create a SolidColorBrush with a red color to fill the
// Ellipse with.
SolidColorBrush mySolidColorBrush = new SolidColorBrush();

// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 255, 0, 0);
myEllipse.Fill = mySolidColorBrush;

// Set the width and height of the Ellipse.
myEllipse.Width = 100;
myEllipse.Height = 100;

// Add the Ellipse to the StackPanel.
myStackPanel.Children.Add(myEllipse);

this.Content = myStackPanel;
}

}
}