要「重複使用」形狀,您可以將樣式定義爲App.xaml
。例如:
你App.xaml
:
<Application x:Class="WpfApp4.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp4"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="YellowEllipseWithBlackBorder" TargetType="Ellipse">
<Setter Property="Fill" Value="Yellow" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="StrokeThickness" Value="5" />
<Setter Property="Stroke" Value="Black" />
</Style>
</Application.Resources>
</Application>
你MainWindow.xaml
:
<Window x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" />
<Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" />
<Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" />
</StackPanel>
</Grid>
</Window>
結果:
![Three ellipses with the same style](https://i.stack.imgur.com/qMPlj.png)
正如你所看到的,你應該定義一個樣式爲你塑造和t母雞從窗口這個語法應用它:<Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" />
如果您希望以編程方式做同樣的,你可以在前面的例子中定義形狀的風格一樣,然後在窗口的後臺代碼:
public partial class MainWindow : Window
{
private StackPanel _stackPanel;
public MainWindow()
{
InitializeComponent();
_stackPanel = new StackPanel()
{
Orientation = Orientation.Horizontal,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
Content = _stackPanel;
AddEllipse();
AddEllipse();
AddEllipse();
}
public void AddEllipse()
{
var ellipse = new Ellipse()
{
Style = FindResource("YellowEllipseWithBlackBorder") as Style
};
_stackPanel.Children.Add(ellipse);
}
}
要繪製自定義形狀,你可以使用Polygon
控制,您可以在其中定義形狀的點,例如:
public void AddPolygon()
{
var polygon = new Polygon()
{
Fill = new SolidColorBrush(Colors.Red),
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 2.0
};
polygon.Points.Add(new Point(0, 0));
polygon.Points.Add(new Point(10, 0));
polygon.Points.Add(new Point(5, -10));
_stackPanel.Children.Add(polygon);
}
結果:
![Polygon](https://i.stack.imgur.com/z9kjg.png)
畫一些曲線,你可以使用Path
控制與BezierSegment
,例如:
public void AddPath()
{
var canvas = new Canvas();
var path = new Path()
{
Fill = new SolidColorBrush(Colors.Red),
Stroke = new SolidColorBrush(Colors.Black),
StrokeThickness = 2.0
};
var geometry = new PathGeometry();
geometry.Figures.Add(new PathFigure(
new Point(0, 0),
new List<BezierSegment>()
{
new BezierSegment(
new Point(0, 0),
new Point(100, 0),
new Point(50, -100),
true)
},
false));
path.Data = geometry;
canvas.Children.Add(path);
_stackPanel.Children.Add(canvas);
}
![Path with BezierSegment](https://i.stack.imgur.com/WM8sk.png)
何不你是不是在尋找和開始一些事情並回到你正面臨的確切問題?另請檢查:[我如何問一個好問題](https://stackoverflow.com/help/how-to-ask) –