最近遇到一个问题,需要在界面上动态生成一些控件/图,于是学习了一些在UI后台用C#代码绘图、添加控件的方法,下面放个小例子:
/// <summary>
/// TimeLine.xaml 的交互逻辑
/// </summary>
public partial class TimeLine : UserControl
{
public TimeLine()
{
InitializeComponent();
DataContext = new TimeLineViewModel();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
//绘制二次方贝塞尔曲线段,并将其添加到Canvas控件中
Path path = new Path
{
Stroke = Brushes.DarkRed,
StrokeThickness = 10,
};
PathGeometry pathGeometry = new PathGeometry();
PathFigure pathFigure = new PathFigure
{
//起节点
StartPoint = new Point(10, -30),
};
QuadraticBezierSegment qbs = new QuadraticBezierSegment
{
//控制节点