2016-05-04 64 views
1

如果是这样的基本问题,我很抱歉。如何在两条平行线WPF之间设置填充?

问题是我应该在画布上绘制两条平行线或两条平行曲线。我想在这两条不相交的线条之间设置一个颜色。我正在使用两个折线来绘制它们。

任何帮助表示赞赏。提前致谢。 代码:

<Canvas.LayoutTransform> 
     <ScaleTransform CenterX="0" CenterY="0" ScaleY="-1" ScaleX="1"/> 
    </Canvas.LayoutTransform> 
    <Polyline Name="MyLine1" Points="{Binding BindPoints1,Mode=TwoWay}" Stroke="Black" StrokeThickness="4" Grid.Row="0" /> 
    <Polyline Name="MyLine2" Points="{Binding BindPoints2,Mode=TwoWay}" Stroke="Black" StrokeThickness="4" Grid.Row="0" /> 

和C#

public class ViewModel : ViewModelBase 
{ 
    private ImageSource m_CreatedImage; 
    public PointCollection BindPoints1 { get; set; } 
    public PointCollection BindPoints2 { get; set; } 


    public ViewModel() 
    { 
     BindPoints1 = new PointCollection(); 
     BindPoints2 = new PointCollection(); 
     for (int i = 0; i < 1000; i++) 
     { 
      double val = (i * i) - 5; 
      var point = new Point(i, i+20);    
      BindPoints1.Add(point); 
     } 

     BindPoints2 = new PointCollection(); 
     for (int i = 0; i < 1000; i++) 
     { 
      double val = (i * i) + 5; 
      var point = new Point(i, i-20); 
      BindPoints2.Add(point); 
     } 
    } 


} 
+0

你最好的选择可能是您的线转换为多边形和弧到圈子那么层和填充它们,这样你得到你想要的外观。 –

回答

0

创建第三个点集合,其中包含第一条线的所有点和第二条线的所有点。 第二行中的点需要颠倒。想象一下,走到一条街的尽头,穿过,然后从另一边回来。

将新的Line绑定到第三组点,并设置Fill而不是Stroke并在绘制其他线/弧之前绘制它。

<Polyline Name="FillLine" Points="{Binding BindPoints3,Mode=TwoWay}" Fill="Green" Grid.Row="0"/> 
<Polyline Name="MyLine1" Points="{Binding BindPoints1,Mode=TwoWay}" Stroke="Black" StrokeThickness="4" Grid.Row="0" /> 
<Polyline Name="MyLine2" Points="{Binding BindPoints2,Mode=TwoWay}" Stroke="Black" StrokeThickness="4" Grid.Row="0" /> 

视图模型:

public class ViewModel : ViewModelBase 
{ 
    private ImageSource m_CreatedImage; 
    public PointCollection BindPoints1 { get; set; } 
    public PointCollection BindPoints2 { get; set; } 
    public PointCollection BindPoints3 { get; set; } 


    public ViewModel() 
    { 
     BindPoints1 = new PointCollection(); 
     BindPoints2 = new PointCollection(); 
     for (int i = 0; i < 1000; i++) 
     { 
      double val = (i * i) - 5; 
      var point = new Point(i, i + 20); 
      BindPoints1.Add(point); 
     } 

     BindPoints2 = new PointCollection(); 
     for (int i = 0; i < 1000; i++) 
     { 
      double val = (i * i) + 5; 
      var point = new Point(i, i - 20); 
      BindPoints2.Add(point); 
     } 
     BindPoints3 = new PointCollection(BindPoints1.OfType<Point>().Concat(BindPoints2.OfType<Point>().Reverse())); 
    } 
} 
1

的最好的事情是定义一个网格,并首先将其划分为4-5行。 在第一行和最后一行添加该行。跨越中间的2-3行,并根据您的要求添加一个表示矩形或椭圆形的形状,并只填充所需的颜色。

检查下面的示例。

<Window x:Class="WpfApplication1.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:WpfApplication1" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="350" Width="525" 
    > 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
      <RowDefinition /> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 

     <Polyline Name="MyLine1" Grid.Row="0" Points="{Binding BindPoints1,Mode=TwoWay}" Stroke="Black" StrokeThickness="4" /> 
      <Polyline Name="MyLine2" Grid.Row="4" Points="{Binding BindPoints2,Mode=TwoWay}" Stroke="Black" StrokeThickness="4" /> 
      <Rectangle Grid.Row="1" Grid.RowSpan="3" Fill="Red" /> 

    </Grid> 
</Window>