WPF C# 自定义坐标轴(X轴,Y轴)

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Canvas x:Name="canvas" Background="Gray"/>
        <Canvas
         x:Name="Canvas_Container" Grid.Column="1"></Canvas>
    </Grid>
    
    /// <summary>
    /// XYUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class XYUserControl : UserControl
    {
        public static List<double> XFloagPotin = new List<double>();
        public XYUserControl()
        {
            InitializeComponent();
            this.Loaded += XYUserControl_Loaded;
            this.SizeChanged += XYUserControl_SizeChanged;
        }
        private void XYUserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Canvas_Container.Children.Clear();
            canvas.Children.Clear();
            DrowXPolyLine();
            DrowYPolyLine();
            CreateTickText();
        }
        private void XYUserControl_Loaded(object sender, RoutedEventArgs e)
        {
            DrowXPolyLine();
            DrowYPolyLine();
            CreateTickText();
        }
        private void DrowXPolyLine()
        {
            double tickWidth = this.Canvas_Container.ActualWidth / 20;
            for (int i = 0; i < 20; i++)
            {
                Polyline XPolyline = new Polyline();
                XPolyline.Stroke = new SolidColorBrush(Colors.Gray);
                XPolyline.StrokeThickness = 1;
                XPolyline.Points.Add(new Point(i * tickWidth, 0));
                XPolyline.Points.Add(new Point(i * tickWidth, this.Canvas_Container.ActualHeight));
                this.Canvas_Container.Children.Add(XPolyline);
            }
        }
        private void DrowYPolyLine()
        {
            double tickHeigt = this.Canvas_Container.ActualHeight / 20;
            for (int i = 0; i < 20; i++)
            {
                Polyline YPolyline = new Polyline();
                YPolyline.Stroke = new SolidColorBrush(Colors.Gray);
                YPolyline.StrokeThickness = 1;
                YPolyline.Points.Add(new Point(0, i * tickHeigt));
                YPolyline.Points.Add(new Point(this.Canvas_Container.ActualWidth, i * tickHeigt));
                this.Canvas_Container.Children.Add(YPolyline);
            }
        }
        private void CreateTickText()
        {
            //int TickLength = Convert.ToInt32(this.canvas.ActualHeight / 17);
            double TickLength = this.canvas.ActualHeight / 17;
            for (int i = 0; i < 17; i++)
            {
                Label label = new Label();
                label.Content = (i * 20).ToString();
                label.FontSize = 10;
                label.HorizontalContentAlignment = HorizontalAlignment.Right;
                label.VerticalContentAlignment = VerticalAlignment.Bottom;
                label.Foreground = new SolidColorBrush(Colors.White);
                label.SetValue(Canvas.BottomProperty, i * TickLength - 10);
                this.canvas.Children.Add(label);
            }
        }
    }

 

WPF C# 自定义坐标轴(X轴,Y轴)

上一篇:Windows下make clean指令错误[错误码2](系统找不到指定文件)的解决方案


下一篇:Dynamics CRM 365 窗体的Lookup字段通过JS按照某个字段过滤数据