2016-09-29 3 views
0

У меня есть лишенный WPF-пример проблемы, с которой я сталкиваюсь в гораздо большем проекте. У меня есть пользовательский элемент управления UserControl1. Контекст данных установлен в self, поэтому у меня есть свойства зависимостей, определенные в коде.Пользовательский элемент управления WPF не обновляет путь

У меня есть элемент управления ItemsControl. В ItemsSource у меня есть CompositeCollection, который содержит CollectionContainer и строку. Линия только для того, чтобы доказать себе, что я рисую.

У меня также есть объект, называемый GraphPen, который содержит свойство зависимостей PathGeometry. Элемент CollectionContainer пользовательского элемента управления содержит ObservableCollection из этих GraphPens.

Теперь у меня есть «MainWindow», чтобы проверить пользовательский элемент управления. В MainWindow у меня есть DispatchTimer и в событии Tick этого таймера, я добавляю LineSegments к PathFigure, который был добавлен в коллекцию FigureG PathMeometry одного экземпляра GraphPen.

Я ожидаю увидеть диагональную линию, проходящую параллельно существующей красной линии, но ничего не появляется. Если я положил точку прерывания в конец обработчика событий 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(); 
     } 
    } 
} 

Screen Shot

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 (сегмент);'] (https://msdn.microsoft.com/en-us/library/system.windows.media.pathsegmentcollection (v = vs.110). ASPX). Я не думаю, что ни одна из этих коллекций не поднимает никаких событий, когда вы меняете их содержимое. –

ответ

1

мне не нужно INotifyPropertyChanged, и мне не нужно, чтобы воссоздать PenGeometry. Извините, я потратил ваше время на эти идеи.

У меня есть рисунок кода ... что-то. Линия, которая растет. Я не знаю, рисует ли он именно то, что вы хотите, но теперь вы можете понять эту часть, что вы можете увидеть, что это такое is рисунок.

Во-первых, незначительная ошибка копирования/вставки в 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 является целью прокси.

Кроме того, нам нужна ItemTemplate для ItemsControl, потому что он не знает, как отобразить 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, и он даст вам много информации об отладке о Binding в панели вывода VS. Я добавил, что когда я пытался понять, почему я устанавливал PenLineColor в Brushes.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)); 
    } 
} 

И, наконец, в MainWindow, вам необходимо пройти true для isStroked на LineSegment случаях:

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)); перегрузки. У меня есть большая аллергия на то, чтобы положить логику в обработчики событий. Самое большее, что я сделаю, это бросить if или try/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

Ред, см. Мои изменения к вашему ответу. Они должны появиться после того, как они будут рассмотрены. - Doug – dtaylor

+0

@dtaylor Принятые изменения, с комментарием. –

Смежные вопросы