当前位置:网站首页>wpf path xaml写法和c#写法对比

wpf path xaml写法和c#写法对比

2022-08-09 11:09:00 一头小驴

转载自:https://www.c-sharpcorner.com/UploadFile/mahesh/path-in-wpf/

wpf path的c#写法,没有找到很多案例,对于很多需要代码去生成 路径点集合 的需求,可能还需要了解一下 path的c#写法。

在这里插入图片描述
xaml

<Window x:Class="_1_5GraphicsWpf.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_1_5GraphicsWpf"
        mc:Ignorable="d"
        Title="Window3" Height="800" Width="1200">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>


        <Canvas x:Name="myCanvas" Grid.Column="0" Background="AliceBlue"></Canvas>
        <Canvas Grid.Column="1">
            <Path Stroke="Black" StrokeThickness="3" Fill="Blue">
                <Path.Data>
                    <GeometryGroup>
                        <LineGeometry StartPoint="20,200" EndPoint="300,200" />
                        <EllipseGeometry Center="80,150" RadiusX="50" RadiusY="50" />
                        <RectangleGeometry Rect="80,167 150 30" />
                    </GeometryGroup>
                </Path.Data>
            </Path>

            <Path Stroke="Black" StrokeThickness="3" Fill="Blue">
                <Path.Data>
                    <EllipseGeometry Center="480,450" RadiusX="50" RadiusY="50" />
                </Path.Data>
            </Path>
        </Canvas>
    </Grid>
</Window>

xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace _1_5GraphicsWpf
{
    /// <summary>
    /// Window3.xaml 的交互逻辑
    /// </summary>
    public partial class Window3 : Window
    {
        System.Windows.Shapes.Path path;
        public Window3()
        {
            
            InitializeComponent();
            CreateAPath();
        }
        void Init()
        {
            Slider slider = new Slider();
        }

        ///<summary>  
        /// Creates a blue path with black stroke  
        ///</summary>  
        public void CreateAPath()
        {
            // Create a blue and a black Brush  
            SolidColorBrush blueBrush = new SolidColorBrush();
            blueBrush.Color = Colors.Blue;
            SolidColorBrush blackBrush = new SolidColorBrush();
            blackBrush.Color = Colors.Black;
            // Create a Path with black brush and blue fill  
            Path bluePath = new Path();
            bluePath.Stroke = blackBrush;
            bluePath.StrokeThickness = 3;
            bluePath.Fill = blueBrush;
            // Create a line geometry  
            LineGeometry blackLineGeometry = new LineGeometry();
            blackLineGeometry.StartPoint = new Point(20, 200);
            blackLineGeometry.EndPoint = new Point(300, 200);
            // Create an ellipse geometry  
            EllipseGeometry blackEllipseGeometry = new EllipseGeometry();
            blackEllipseGeometry.Center = new Point(80, 150);
            blackEllipseGeometry.RadiusX = 50;
            blackEllipseGeometry.RadiusY = 50;
            // Create a rectangle geometry  
            RectangleGeometry blackRectGeometry = new RectangleGeometry();
            Rect rct = new Rect();
            rct.X = 80;
            rct.Y = 167;
            rct.Width = 150;
            rct.Height = 30;
            blackRectGeometry.Rect = rct;
            // Add all the geometries to a GeometryGroup.  
            GeometryGroup blueGeometryGroup = new GeometryGroup();
            blueGeometryGroup.Children.Add(blackLineGeometry);
            blueGeometryGroup.Children.Add(blackEllipseGeometry);
            blueGeometryGroup.Children.Add(blackRectGeometry);
            // Set Path.Data  
            bluePath.Data = blueGeometryGroup;
            myCanvas.Children.Add(bluePath);
        }
        }
}

原网站

版权声明
本文为[一头小驴]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_37326058/article/details/104188104