如何绑定linesements指向WPF中的xml数据

时间:2014-07-17 06:30:35

标签: c# wpf

我有一些数据

<Array>
    <Element Value="30"/>
    <Element Value="50"/>
    <Element Value="10"/>
</Array>

现在我想使用这些数据创建一条曲线。我想要使​​用LineSegments。但我无法理解如何将LineSegment点绑定到此数据?

我的意思是,是否有任何语法有助于编写而不是

<GeometryDrawing.Geometry>
  <PathGeometry>
    <PathFigure>
      <LineSegment Point="0,30"/>
      <LineSegment Point="20,50"/>
      <LineSegment Point="40,10"/>
    </PathFigure>
  </PathGeometry>
</GeometryDrawing.Geometry>

类似的东西:

<GeometryDrawing.Geometry>
  <PathGeometry>
    <PathFigure>
      <LineSegment Point={Binding ????}/>
    </PathFigure>
  </PathGeometry>
</GeometryDrawing.Geometry>

主要问题是如何使用绑定将坐标绑定到linesegment的点结构。

提前致谢!

1 个答案:

答案 0 :(得分:0)

您可以简单地使用LineSegment来代替将数组转换为PolyLineSegment集合。如果Binding只需要工作(将点数组转换为PointCollection的{​​{1}},之后对点数组进行的每次更改都不会触发更新到PolyLineSegment),您可以这样做:

PolyLineSegment

以下是用于将点数组转换为public MainWindow(){ InitializeComponent(); //set DataContext //Initialize your Points here ... //... DataContext = this; } public Point[] Points {get;private set;} 的转换器:

PointCollection

然后在XAML代码中:

public class PointArrayToPointCollection : IValueConverter {
   object IValueConverter.Convert(object value, Type targetType, 
                                  object parameter, CultureInfo culture) {
      var points = value as IEnumerable<Point>;
      if(points != null) return new PointCollection(points);
      return Binding.DoNothing;
   }
   object IValueConverter.ConvertBack(object value, Type targetType,
                                      object parameter, CultureInfo culture) {
      throw new NotImplementedException();
   }
}

这个答案可能无法完全解决您的问题,但使用<Window x:Class="yourNameSpace.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:yourNameSpace"/> <Window.Resources> <local:PointArrayToPointCollection x:Key="pointsToPointCollection"/> </Window.Resources> <Image> <Image.Source> <DrawingImage> <DrawingImage.Drawing> <GeometryDrawing> <GeometryDrawing.Pen> <Pen Brush="Red" Thickness="1"></Pen> </GeometryDrawing.Pen> <GeometryDrawing.Geometry> <PathGeometry> <PathFigure> <PolyLineSegment Points="{Binding Points, Converter={StaticResource pointsToPointCollection}}"/> </PathFigure> </PathGeometry> </GeometryDrawing.Geometry> </DrawingImage.Drawing> </DrawingImage> </Image.Source> </Image> </Window> 的想法应该指向正确的方向。

<强>更新

嗯,我只是觉得你需要在实际应用程序中运行一些实际的代码。如果您只需要在 KXAML 编辑器中测试(或使用)XAML代码,那么您没有理由在此处使用XML数据。因为PolyLineSegment的{​​{1}}属性需要PointCollection,所以使用任何其他类型的数据都需要进行一些转换(只能通过后面的代码完成)。所以我们只需要在XAML代码中指定Points作为实例,它可以是这样的:

PolyLineSegment

据我所知,你可能有更多连续点的集合(可以形成近乎弯曲的线条)。如果你想要一些完美的曲线,你可以尝试使用PointCollection<Page.Resources> <PointCollection x:Key="points"> <Point>0,30</Point> <Point>20,50</Point> <Point>40,10</Point> </PointCollection> </Page.Resources> <Grid> <Grid.Background> <DrawingBrush> <DrawingBrush.Drawing> <GeometryDrawing> <GeometryDrawing.Pen> <Pen Brush="Red" Thickness="1"></Pen> </GeometryDrawing.Pen> <GeometryDrawing.Geometry> <PathGeometry> <PathFigure> <PolyLineSegment Points="{StaticResource points}"></PolyLineSegment> </PathFigure> </PathGeometry> </GeometryDrawing.Geometry> </GeometryDrawing> </DrawingBrush.Drawing> </DrawingBrush> </Grid.Background> </Grid> BezierSegmentPolyBezierSegment这样的游戏:

QuadraticBezierSegment

或者您甚至可以尝试使用所谓的迷你Path语言来定义PathGeometry,如下所示:

PolyQuadraticBezierSegment

迷你路径语言由于隐式<Grid> <Grid.Background> <DrawingBrush Stretch="None" Viewport="0.2,0.2,0.6,0.6"> <DrawingBrush.Drawing> <GeometryDrawing> <GeometryDrawing.Pen> <Pen Brush="Red" Thickness="1"></Pen> </GeometryDrawing.Pen> <GeometryDrawing.Geometry> <PathGeometry> <PathFigure> <PolyBezierSegment Points="10,10 200,150 300,30 320,10 330,-100 20,30"> </PolyBezierSegment> </PathFigure> </PathGeometry> </GeometryDrawing.Geometry> </GeometryDrawing> </DrawingBrush.Drawing> </DrawingBrush> </Grid.Background> </Grid> 而起作用。您可以将此迷你语言用于<Grid> <Grid.Background> <DrawingBrush Stretch="None" Viewport="0.2,0.2,0.6,0.6"> <DrawingBrush.Drawing> <GeometryDrawing Geometry="M0,0 C10,10 200,150 300,30 320,10 330,-100 20,30"> <GeometryDrawing.Pen> <Pen Brush="Red" Thickness="1"></Pen> </GeometryDrawing.Pen> </GeometryDrawing> </DrawingBrush.Drawing> </DrawingBrush> </Grid.Background> </Grid> (由GeometryConverter转换)和PathFigureCollection形状的PathFigureCollectionConverter属性。