How can I paint my Polyline like this in some different color in Canvas?

Shawn Song 41 Reputation points
2021-02-25T14:59:24.333+00:00

72077-%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE-2021-02-25-225659.png

怎样在Canvas的Polyline中把部分点设置成不同的颜色?

Dynamic move red part may be better
Thanks

如果能动态设置红色部分就更好了

there is one usercontrol:

<UserControl x:Name="userControl" x:Class="WPFControlLibrary.WPFControl_WaveItem"  
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"   
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
             xmlns:local="clr-namespace:WPFControlLibrary"  
             mc:Ignorable="d"   
             d:DesignHeight="100" d:DesignWidth="800">  
  <Grid>  
        <Grid.RowDefinitions>  
            <RowDefinition Height="{Binding WaveStartDistance,ElementName=userControl,Mode=OneWay}"/>  
            <RowDefinition x:Name="WaveGrid"/>  
        </Grid.RowDefinitions>  
        <Grid.ColumnDefinitions>  
            <ColumnDefinition Width="{Binding MidGridLength, ElementName=userControl,Mode=OneWay}"/>  
            <ColumnDefinition/>  
        </Grid.ColumnDefinitions>  
        <TextBlock Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Foreground="#2C5FFE"  FontSize="13" Text="{Binding WaveName, ElementName=userControl,Mode=OneWay}"/>  
        <Canvas Grid.Row="1" Grid.ColumnSpan="2">  
            <Polyline x:Name="Wave" Canvas.Top="0" Canvas.Left="0" StrokeThickness="1" Stroke="Black"/>  
        </Canvas>  
    </Grid>  
</UserControl>  

and I use this usercontrol in C# code:

PointCollection points = new PointCollection;
//I fill the collection with Math.Sin here
Wave.Points = points;

and I add this usercontrol into one simple Canvas :

canvas.Children.Add(userControl);

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
{count} votes

Accepted answer
  1. Viorel 112.7K Reputation points
    2021-02-26T09:44:41.927+00:00

    Maybe define two polylines and use clipping. Something like this:

    <Canvas . . .>
        <Polyline x:Name="Wave1" Canvas.Top="0" Canvas.Left="0" StrokeThickness="1" Stroke="Black">
            <Polyline.Clip>
                <GeometryGroup>
                    <RectangleGeometry Rect="0 0 200 2000"/>
                    <RectangleGeometry Rect="300 0 2000 2000"/>
                </GeometryGroup>
            </Polyline.Clip>
        </Polyline>
        <Polyline x:Name="Wave2" Canvas.Top="0" Canvas.Left="0" StrokeThickness="1" Stroke="Red">
            <Polyline.Clip>
                <RectangleGeometry Rect="200 0 300 2000"/>
            </Polyline.Clip>
        </Polyline>
    </Canvas>
    

    Then execute:

    Wave1.Points = points;
    Wave2.Points = points;
    

    If you assign some names to these rectangles, then probably you can change the areas (Rect properties) programmatically. Maybe binding will work too.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful