2016-09-29 82 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接受編輯,帶評論。 –