2016-09-29 157 views
0

我有一个简化的WPF示例,我在一个更大的项目中遇到了一个问题。我有一个名为“UserControl1”的用户控件。数据上下文设置为self,因此我在代码隐藏中定义了依赖项属性。WPF用户控件不更新路径

在控件中我有一个ItemsControl。在ItemsSource中,我有一个CompositeCollection,它包含一个CollectionContainer和一个Line。这条线只是为了向我自己证明我正在绘画。

我也有一个叫做“GraphPen”的对象,它包含一个PathGeometry依赖项属性。用户控件的CollectionContainer包含这些GraphPens的ObservableCollection。

现在,我有一个“MainWindow”来测试用户控件。在MainWindow中,我有一个DispatchTimer,并且在该计时器的Tick事件中,我将LineSegments添加到已添加到GraphPen单一实例的PathGeometry的Figures集合的PathFigure中。

我希望看到一条对角线与现有的红线并行绘制,但没有任何显示。如果我在Tick事件处理程序的末尾放置了一个断点,我可以检查用户控件并深入了解并确定线段确实存在。由于某些原因,他们没有被渲染。我怀疑我在绑定中做了什么错误。

我会提供下面的代码。

GraphPen.cs

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Media; 

namespace WpfExampleControlLibrary 
{ 
    public class GraphPen : DependencyObject 
    { 
     #region Constructor 

     public GraphPen() 
     { 
      PenGeometry = new PathGeometry(); 
     } 

     #endregion Constructor 

     #region Dependency Properties 

     // Line Color 

     public static PropertyMetadata PenLineColorPropertyMetadata 
      = new PropertyMetadata(null); 
     public static DependencyProperty PenLineColorProperty 
      = DependencyProperty.Register(
       "PenLineColor", 
       typeof(Brush), 
       typeof(GraphPen), 
       PenLineColorPropertyMetadata); 
     public Brush PenLineColor 
     { 
      get { return (Brush)GetValue(PenLineColorProperty); } 
      set { SetValue(PenLineColorProperty, value); } 
     } 

     // Line Thickness 

     public static PropertyMetadata PenLineThicknessPropertyMetadata 
      = new PropertyMetadata(null); 
     public static DependencyProperty PenLineThicknessProperty 
      = DependencyProperty.Register(
       "PenLineThickness", 
       typeof(Int32), 
       typeof(GraphPen), 
       PenLineThicknessPropertyMetadata); 
     public Int32 PenLineThickness 
     { 
      get { return (Int32)GetValue(PenLineThicknessProperty); } 
      set { SetValue(PenLineThicknessProperty, value); } 
     } 

     // Pen Geometry 

     public static PropertyMetadata PenGeometryMetadata = new PropertyMetadata(null); 
     public static DependencyProperty PenGeometryProperty 
      = DependencyProperty.Register(
       "PenGeometry", 
       typeof(PathGeometry), 
       typeof(UserControl1), 
       PenGeometryMetadata); 

     public PathGeometry PenGeometry 
     { 
      get { return (PathGeometry)GetValue(PenGeometryProperty); } 
      set { SetValue(PenGeometryProperty, value); } 
     } 

     #endregion Dependency Properties 
    } 
} 

UserControl1.xaml

<UserControl Name="ExampleControl" 
      x:Class="WpfExampleControlLibrary.UserControl1" 
      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:WpfExampleControlLibrary" 
      DataContext="{Binding RelativeSource={RelativeSource Self}}" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 

     <DataTemplate DataType="{x:Type local:GraphPen}"> 
      <Path Stroke="{Binding Path=PenLineColor}" 
        StrokeThickness="{Binding Path=PenLineThickness}" 
        Data="{Binding Path=Geometry}"> 
      </Path> 
     </DataTemplate> 

    </UserControl.Resources> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition Height="40"/> 
     </Grid.RowDefinitions> 
     <ItemsControl Grid.Column="0" Grid.Row="0"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <Canvas Background="Aquamarine"> 
         <Canvas.LayoutTransform> 
          <ScaleTransform ScaleX="1" ScaleY="-1" CenterX=".5" CenterY=".5"/> 
         </Canvas.LayoutTransform> 
        </Canvas> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemsSource> 
       <CompositeCollection> 
        <CollectionContainer 
         Collection="{ 
          Binding Source={RelativeSource Self}, 
          Path=GraphPens, 
          Mode=OneWay}"/> 
        <Line X1="10" Y1="0" X2="200" Y2="180" Stroke="DarkRed" StrokeThickness="2"/> 
       </CompositeCollection> 
      </ItemsControl.ItemsSource> 
     </ItemsControl> 
     <TextBox x:Name="debug" Grid.Column="0" Grid.Row="1" Text="{Binding Path=DebugText}"/> 
    </Grid> 
</UserControl> 

UserControl1.xaml.cs

namespace WpfExampleControlLibrary 
{ 
    /// <summary> 
    /// Interaction logic for UserControl1.xaml 
    /// </summary> 
    public partial class UserControl1 : UserControl 
    { 
     public UserControl1() 
     { 
      InitializeComponent(); 

      GraphPens = new ObservableCollection<GraphPen>(); 
     } 

     #region Dependency Properties 

     // Pens 

     public static PropertyMetadata GraphPenMetadata = new PropertyMetadata(null); 
     public static DependencyProperty GraphPensProperty 
      = DependencyProperty.Register(
       "GraphPens", 
       typeof(ObservableCollection<GraphPen>), 
       typeof(UserControl1), 
       GraphPenMetadata); 

     public ObservableCollection<GraphPen> GraphPens 
     { 
      get { return (ObservableCollection<GraphPen>)GetValue(GraphPensProperty); } 
      set { SetValue(GraphPensProperty, value); } 
     } 

     // Debug Text 

     public static PropertyMetadata DebugTextMetadata = new PropertyMetadata(null); 
     public static DependencyProperty DebugTextProperty 
      = DependencyProperty.Register(
       "DebugText", 
       typeof(string), 
       typeof(UserControl1), 
       DebugTextMetadata); 

     public string DebugText 
     { 
      get { return (string)GetValue(DebugTextProperty); } 
      set { SetValue(DebugTextProperty, value); } 
     } 

     #endregion Dependency Properties 
    } 
} 

MainWindow.xaml

<Window x:Class="POC_WPF_UserControlExample.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:Exmpl="clr-namespace:WpfExampleControlLibrary;assembly=WpfExampleControlLibrary" 
     xmlns:local="clr-namespace:POC_WPF_UserControlExample" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="550" Width="550"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="100"/> 
      <ColumnDefinition /> 
      <ColumnDefinition Width="100"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="100" /> 
      <RowDefinition /> 
      <RowDefinition Height="100" /> 
     </Grid.RowDefinitions> 
     <Exmpl:UserControl1 Grid.Column="1" Grid.Row="1" x:Name="myExample"/> 
    </Grid> 
</Window> 

MainWindow.xaml.cs

namespace POC_WPF_UserControlExample 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private DispatcherTimer _timer = null; 
     private GraphPen _graphPen0 = null; 
     private Int32 _pos = 0; 
     private PathFigure _pathFigure = null; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      _graphPen0 = new GraphPen(); 
      _graphPen0.PenLineColor = Brushes.DarkGoldenrod; 
      _graphPen0.PenLineThickness = 2; 
      myExample.GraphPens.Add(_graphPen0); 

      _timer = new DispatcherTimer(); 
      _timer.Tick += Timer_Tick; 
      _timer.Interval = new TimeSpan(0, 0, 0, 0, 100); 
      _timer.Start(); 
     } 

     private void Timer_Tick(object sender, EventArgs e) 
     { 
      _pos++; 
      Point penPoint0 = new Point(_pos, _pos + 20); 
      if (_graphPen0.PenGeometry.Figures.Count == 0) 
      { 
       _pathFigure = new PathFigure(); 
       _graphPen0.PenGeometry.Figures.Add(_pathFigure); 
       _pathFigure.StartPoint = penPoint0; 
      } 
      else 
      { 
       LineSegment segment = new LineSegment(penPoint0, false); 
       _pathFigure.Segments.Add(segment); 
      } 
      myExample.DebugText = _pos.ToString(); 
     } 
    } 
} 

屏幕截图

The line being drawn should be parallel to the red line

+0

你尝试更换GraphPen,而不是改变现有的? –

+0

埃德,感谢您的快速回复。刚刚尝试过,但没有喜悦。我很高兴那不是答案。在这个大项目中,我将有数十支笔每10毫秒更新一次线。我会讨厌每次重新创建笔的开销。我会认为ObservableCollection会为我处理这个问题。 – dtaylor

+0

当您向其中添加或删除项目时,ObservableCollection会引发事件。它不知道它包含的项目的属性是怎么回事。这里的关键是['_graphPen0.PenGeometry.Figures.Add(_pathFigure);'](https://msdn.microsoft.com/en-us/library/system.windows.media.pathfigurecollection(v = vs.110 ).aspx)和['_pathFigure.Segments.Add(segment);'](https://msdn.microsoft.com/en-us/library/system.windows.media.pathsegmentcollection(v = vs.110))。 ASPX)。我不认为这些藏品中的任何一个在您更改其内容时都会举办任何活动。 –

回答

1

我并不需要INotifyPropertyChanged,我没有需要重新PenGeometry。对不起,我用这些想法浪费了你的时间。

我已经得到你的代码绘图...东西。一条生长线。我不知道它是否正在绘制你想要的,但你现在可以找出那部分,你可以看到它是什么绘图。

首先,未成年人复制/粘贴在GraphPen.cs错误:

public static DependencyProperty PenGeometryProperty 
     = DependencyProperty.Register(
      "PenGeometry", 
      typeof(PathGeometry), 
      typeof(UserControl1), 
      PenGeometryMetadata); 

所有者类型参数需要GraphPen,不UserControl1

  typeof(PathGeometry), 
      typeof(GraphPen), 
      PenGeometryMetadata); 

第二:UserControl1绑定。您与Self的绑定不起作用,因为Self(在这种情况下)是您绑定的CollectionContainer。通常你会使用RelativeSource={RelativeSource AncestorType=UserControl}的来源,但CollectionContainer不在视觉树中,因此不起作用(真正直观,呵呵?)。相反,我们使用BindingProxy(源跟随):

<UserControl.Resources> 
    <!-- ... stuff ... --> 

    <local:BindingProxy 
     x:Key="UserControlBindingProxy" 
     Data="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" 
     /> 
</UserControl.Resources> 

而且对于收集容器...

<CollectionContainer 
    Collection="{ 
     Binding 
     Source={StaticResource UserControlBindingProxy}, 
     Path=Data.GraphPens, 
     Mode=OneWay}" 
    /> 

通知我们结合Data.GraphPens; Data是代理的目标。

此外,我们需要一个ItemsControlItemTemplate,因为它不知道如何显示GraphPen

<ItemsControl.ItemTemplate> 
    <DataTemplate DataType="local:GraphPen"> 
     <Border > 
      <Path 
       Data="{Binding PenGeometry}" 
       StrokeThickness="{Binding PenLineThickness}" 
       Stroke="{Binding PenLineColor, PresentationTraceSources.TraceLevel=None}" 
       /> 
     </Border> 
    </DataTemplate> 
</ItemsControl.ItemTemplate> 

PresentationTraceSources.TraceLevel=None。将None更改为High,它会给你很多有关VS输出窗格中的Binding的调试信息。我补充说,当我试图弄清楚为什么我在构造函数中设置了PenLineColorBrushes.Black,但它在运行时不断出现DarkGoldenrod。你能说duhhh吗? Duhhh!

现在是绑定代理。这样做的作用是将任意想要使用的对象作为DataContext,将其绑定到Data上定义为资源的BindingProxy实例,并且您可以通过资源获得DataContext,该资源可通过StaticResource获得。如果您在某个地方无法通过与RelativeSource的视觉树进行搜索,那么您可以依赖这个选项。

BindingProxy.cs

using System.Windows; 

namespace WpfExampleControlLibrary 
{ 
    public class BindingProxy : Freezable 
    { 
     #region Overrides of Freezable 

     protected override Freezable CreateInstanceCore() 
     { 
      return new BindingProxy(); 
     } 

     #endregion 

     public object Data 
     { 
      get { return (object)GetValue(DataProperty); } 
      set { SetValue(DataProperty, value); } 
     } 

     // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty DataProperty = 
      DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); 
    } 
} 

最后,在主窗口,你需要传递trueisStrokedLineSegment实例:

LineSegment segment = new LineSegment(penPoint0, true); 

否则他们不会绘制。

所以现在它回到了你的腿上,在温暖的黄金和舒缓的海蓝宝石。 Ave,imperator,和所有。

编辑原作者!

谢谢你的所有努力。

  1. 我无法相信我错过了的UserControl1 - > GraphPen
  2. 的BindingProxy将是非常方便的
  3. 的的TraceLevel也将是得心应手,我没有用之前
  4. 我还纠正了DebugText绑定,现在工作

    我从来没有注意到!

  5. 在DataTemplate中,为什么我们需要将路径包装在边框中?

    我们没有。在我看到Path显示之前,我补充说要在模板中保证可见的内容。它最初有一个绿色边框。我删除了这些属性,但忘记删除Border本身。

  6. 如果你看我的Timer_Tick你会注意到,现在我需要更新的是添加新的段。希望这会有助于表现。你的意见?

    不知道。我实际上会把该段添加GraphPen中的代码作为AddSegment(Point pt)AddSegment(float x, float y) => AddSegment(new Point(x,y));重载。我对将事件处理程序中的逻辑进行了很好的过敏。我要做的最多的是围绕非处理器方法抛出一个iftry/catch,这个方法完成真正的工作。然后,我会写AddSegment(Point pt)两种方式,并相互指责。

我将添加我的代码的完整性:

UserControl1.xaml

<UserControl Name="ExampleControl" 
      x:Class="WpfExampleControlLibrary.UserControl1" 
      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:WpfExampleControlLibrary" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 

     <local:BindingProxy 
      x:Key="UserControlBindingProxy" 
      Data="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}"/> 

     <DataTemplate DataType="{x:Type local:GraphPen}"> 
      <Border> 
       <Path 
        Data="{Binding PenGeometry}" 
        StrokeThickness="{Binding PenLineThickness}" 
        Stroke="{Binding 
         PenLineColor, 
         PresentationTraceSources.TraceLevel=None}" 
        /> 
      </Border> 
     </DataTemplate> 

    </UserControl.Resources> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition Height="40"/> 
     </Grid.RowDefinitions> 
     <ItemsControl Grid.Column="0" Grid.Row="0"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <Canvas Background="Aquamarine"> 
         <Canvas.LayoutTransform> 
          <ScaleTransform ScaleX="1" ScaleY="-1" CenterX=".5" CenterY=".5"/> 
         </Canvas.LayoutTransform> 
        </Canvas> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemsSource> 
       <CompositeCollection> 
        <CollectionContainer 
         Collection="{Binding 
          Source={StaticResource UserControlBindingProxy}, 
          Path=Data.GraphPens, 
          Mode=OneWay}"/> 
        <Line X1="10" Y1="0" X2="200" Y2="180" Stroke="DarkRed" StrokeThickness="2"/> 
       </CompositeCollection> 
      </ItemsControl.ItemsSource> 
     </ItemsControl> 
     <TextBox 
      x:Name="debug" 
      Grid.Column="0" Grid.Row="1" 
      Text="{Binding 
       Source={StaticResource UserControlBindingProxy}, 
       Path=Data.DebugText, 
       Mode=OneWay}"/> 
    </Grid> 
</UserControl> 

UserControl1.xaml.cs

namespace WpfExampleControlLibrary 
{ 
    /// <summary> 
    /// Interaction logic for UserControl1.xaml 
    /// </summary> 
    public partial class UserControl1 : UserControl 
    { 
     #region Constructor 

     public UserControl1() 
     { 
      InitializeComponent(); 

      GraphPens = new ObservableCollection<GraphPen>(); 
     } 

     #endregion Constructor 

     #region Public Methods 

     #endregion Public Methods 

     #region Dependency Properties 

     // Pens 

     public static PropertyMetadata GraphPenMetadata = new PropertyMetadata(null); 
     public static DependencyProperty GraphPensProperty 
      = DependencyProperty.Register(
       "GraphPens", 
       typeof(ObservableCollection<GraphPen>), 
       typeof(UserControl1), 
       GraphPenMetadata); 

     public ObservableCollection<GraphPen> GraphPens 
     { 
      get { return (ObservableCollection<GraphPen>)GetValue(GraphPensProperty); } 
      set { SetValue(GraphPensProperty, value); } 
     } 

     // Debug Text 

     public static PropertyMetadata DebugTextMetadata = new PropertyMetadata(null); 
     public static DependencyProperty DebugTextProperty 
      = DependencyProperty.Register(
       "DebugText", 
       typeof(string), 
       typeof(UserControl1), 
       DebugTextMetadata); 

     public string DebugText 
     { 
      get { return (string)GetValue(DebugTextProperty); } 
      set { SetValue(DebugTextProperty, value); } 
     } 

     #endregion Dependency Properties 
    } 
} 

GraphPen.cs

namespace WpfExampleControlLibrary 
{ 
    public class GraphPen : DependencyObject 
    { 
     #region Constructor 

     public GraphPen() 
     { 
      PenGeometry = new PathGeometry(); 
     } 

     #endregion Constructor 

     #region Dependency Properties 

     // Line Color 

     public static PropertyMetadata PenLineColorPropertyMetadata 
      = new PropertyMetadata(null); 
     public static DependencyProperty PenLineColorProperty 
      = DependencyProperty.Register(
       "PenLineColor", 
       typeof(Brush), 
       typeof(GraphPen), 
       PenLineColorPropertyMetadata); 
     public Brush PenLineColor 
     { 
      get { return (Brush)GetValue(PenLineColorProperty); } 
      set { SetValue(PenLineColorProperty, value); } 
     } 

     // Line Thickness 

     public static PropertyMetadata PenLineThicknessPropertyMetadata 
      = new PropertyMetadata(null); 
     public static DependencyProperty PenLineThicknessProperty 
      = DependencyProperty.Register(
       "PenLineThickness", 
       typeof(Int32), 
       typeof(GraphPen), 
       PenLineThicknessPropertyMetadata); 
     public Int32 PenLineThickness 
     { 
      get { return (Int32)GetValue(PenLineThicknessProperty); } 
      set { SetValue(PenLineThicknessProperty, value); } 
     } 

     // Pen Geometry 

     public static PropertyMetadata PenGeometryMetadata = new PropertyMetadata(null); 
     public static DependencyProperty PenGeometryProperty 
      = DependencyProperty.Register(
       "PenGeometry", 
       typeof(PathGeometry), 
       typeof(GraphPen), 
       PenGeometryMetadata); 

     public PathGeometry PenGeometry 
     { 
      get { return (PathGeometry)GetValue(PenGeometryProperty); } 
      set { SetValue(PenGeometryProperty, value); } 
     } 

     #endregion Dependency Properties 
    } 
} 

BindingProxy.cs

namespace WpfExampleControlLibrary 
{ 
    public class BindingProxy : Freezable 
    { 
     #region Override Freezable Abstract Parts 
     protected override Freezable CreateInstanceCore() 
     { 
      return new BindingProxy(); 
     } 

     #endregion Override Freezable Abstract Parts 

     #region Dependency Properties 

     // Using a DependencyProperty as the backing store for Data. 
     // This enables animation, styling, binding, etc... 
     public static PropertyMetadata DataMetadata = new PropertyMetadata(null); 
     public static readonly DependencyProperty DataProperty 
      = DependencyProperty.Register(
       "Data", 
       typeof(object), 
       typeof(BindingProxy), 
       DataMetadata); 

     public object Data 
     { 
      get { return (object)GetValue(DataProperty); } 
      set { SetValue(DataProperty, value); } 
     } 

     #endregion Dependency Properties 
    } 
} 

MainWindow.xaml

<Window x:Class="POC_WPF_UserControlExample.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:Exmpl="clr-namespace:WpfExampleControlLibrary;assembly=WpfExampleControlLibrary" 
     xmlns:local="clr-namespace:POC_WPF_UserControlExample" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="550" Width="550"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="100"/> 
      <ColumnDefinition /> 
      <ColumnDefinition Width="100"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="100" /> 
      <RowDefinition /> 
      <RowDefinition Height="100" /> 
     </Grid.RowDefinitions> 
     <Exmpl:UserControl1 Grid.Column="1" Grid.Row="1" x:Name="myExample"/> 
    </Grid> 
</Window> 

MainWindow.xaml.cs

namespace POC_WPF_UserControlExample 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     private DispatcherTimer _timer = null; 
     private GraphPen _graphPen0 = null; 
     private Int32 _pos = 0; 
     private PathFigure _pathFigure0 = null; 
     private bool _firstTime = true; 

     public MainWindow() 
     { 
      InitializeComponent(); 

      _pathFigure0 = new PathFigure(); 
      _graphPen0 = new GraphPen(); 
      _graphPen0.PenLineColor = Brushes.DarkGoldenrod; 
      _graphPen0.PenLineThickness = 2; 
      _graphPen0.PenGeometry = new PathGeometry(); 
      _graphPen0.PenGeometry.Figures.Add(_pathFigure0); 
      myExample.GraphPens.Add(_graphPen0); 

      _timer = new DispatcherTimer(); 
      _timer.Tick += Timer_Tick; 
      _timer.Interval = new TimeSpan(0, 0, 0, 0, 100); 
      _timer.Start(); 
     } 

     private void Timer_Tick(object sender, EventArgs e) 
     { 
      _pos++; 
      Point penPoint0 = new Point(_pos, _pos + 20); 
      if (_firstTime) 
      { 
       myExample.GraphPens[0].PenGeometry.Figures[0].StartPoint = penPoint0; 
       _firstTime = false; 
      } 
      else 
      { 
       LineSegment segment = new LineSegment(penPoint0, true); 
       myExample.GraphPens[0].PenGeometry.Figures[0].Segments.Add(segment); 
      } 

      myExample.DebugText = _pos.ToString(); 
     } 
    } 
} 
+0

埃德,你是王子。我今天不得不离开,但我明天会跳到这里。再次感谢。当我完成后我会更新它。 – dtaylor

+0

埃德,看我的编辑到你的答案。他们应该在他们被审查后出现。 - 道格 – dtaylor

+0

@dtaylor接受编辑,带评论。 –