(三)WPF使用 Polyline 元素动态绘制折线(添加背景单元格)

前端代码

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.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace 使用_Polyline_元素来绘制折线
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private int currentSecond = 0;
        private double xGrap = 20;//x坐标的间隔
        private int xPagePoint = 20;//每一页要显示多少个数据点
        Random rd = new Random();
        private DispatcherTimer dispatcherTimer = new DispatcherTimer();
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);//1秒钟产生一次中断
            dispatcherTimer.Tick += timer_Tick;//中断入口函数
            dispatcherTimer.IsEnabled = true;//开启中断
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            currentSecond++;
            double x = currentSecond * xGrap;
            double y = rd.Next(1,200);
                    
            var point = new Point(x, y);
            _myPolyline.Points.Add(point);//添加新的数据点

            if (currentSecond > xPagePoint)
            {
                _myPolyline.Points.RemoveAt(0);
                _myCanvas.Margin = new Thickness(-1*(xGrap * currentSecond)+ xPagePoint * xGrap, 0, 0, 0);
            }
        }
    }
}
 

MainWindow.xaml的代码如下:

<Window x:Class="使用_Polyline_元素来绘制折线.MainWindow"
        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:使用_Polyline_元素来绘制折线"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid >
        <Border Style="{StaticResource MyGridBorderStyle}" Height="409" Margin="0,0,43,0" Width="749">
            <Canvas Name="_myCanvas" Margin="0,0,0,0" >

                <Polyline Name="_myPolyline"
            Stroke="Red"
            StrokeThickness="1" />
            </Canvas>
        </Border>
        <Button Content="开始"  Click="Button_Click"  HorizontalAlignment="Right" VerticalAlignment="Top" />
    </Grid>
</Window>


App.xaml的代码如下:

<Application x:Class="使用_Polyline_元素来绘制折线.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:使用_Polyline_元素来绘制折线"
             StartupUri="MainWindow.xaml">
    <Application.Resources>

        <DrawingBrush x:Key="MyGridBrushResource" Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing Brush="White">
                            <GeometryDrawing.Geometry>
                                <RectangleGeometry Rect="0,0,1,1" />
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                        <GeometryDrawing Geometry="M0,0 L1,0 1,0.1, 0,0.1Z" Brush="#CCCCFF" />
                        <GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="#CCCCFF" />
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
        <Style x:Key="MyGridBorderStyle">
            <Setter Property="Border.Background" Value="{StaticResource MyGridBrushResource}"/>
            <Setter Property="Border.HorizontalAlignment" Value="Center"/>
            <Setter Property="Border.VerticalAlignment" Value="Top"/>
            <Setter Property="Border.BorderBrush" Value="Black"/>
            <Setter Property="Border.BorderThickness" Value="1"/>
        </Style>
    </Application.Resources>
</Application>
 

其实就是利用静态资源来实现背景单元格的

猜你喜欢

转载自blog.csdn.net/u011240538/article/details/85464730