0

私の目標は2秒ごとにポイントを追加できるポリラインを持つことです.PolyLineはそれをUIに反映します。WPFでPolyLineを動的に更新する方法は?

私は次のように値コンバータを使用してのObservableCollectionにバインドされているポリラインあります

XAML

<Polyline Points="{Binding OutCollection,Mode=TwoWay,Converter={StaticResource pointConverter}}" Stroke="Blue" StrokeThickness="15" Name="Line2" /> 

値コンバータ

public class PointCollectionConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     PointCollection outCollection = new PointCollection(); 
     if (value is ObservableCollection<Point>) 
     { 
      var inCollection = value as ObservableCollection<Point>; 

      foreach (var item in inCollection) 
      { 
       outCollection.Add(new Point(item.X, item.Y)); 
      } 
      return outCollection; 
     } 
     else 
      throw new Exception("Wrong Input"); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new Exception("Wrong Operation"); 
    } 
} 

そして、分離コードファイル内:

public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = vm; 
     InitTimer(); 
    } 

    private void InitTimer() 
    { 
     dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); 
     dispatcherTimer.Tick += new EventHandler(TickHandler); 
     dispatcherTimer.Interval = new TimeSpan(0, 0, 2); 
     dispatcherTimer.Start(); 
    } 
    int counter = 0; 
    private void TickHandler(object sender, EventArgs e) 
    { 
     double val = (counter * counter) * (0.001); 
     var point = new Point(counter, val); 
     vm.OutCollection.Add(point); 
     vm.OutCollection.CollectionChanged += OutCollection_CollectionChanged;   

     counter++; 
    } 

のViewModel:

public class ViewModel : ViewModelBase 
{ 
    public ObservableCollection<Point> OutCollection 
    { 
     get 
     { 
      return m_OutCollection; 
     } 

     set 
     { 
      m_OutCollection = value; 
      this.OnPropertyChanged("OutCollection"); 
     } 
    } 

}

private ObservableCollection<Point> m_OutCollection; 


    public ViewModel() 
    { 

     OutCollection = new ObservableCollection<Point>(); 

    } 

そのが働いていません。どんな助けもありがたいですか?

答えて

0

私はそれを自分で解決しました。

のObservableCollectionには役立ちますが、どのような助けはTickHandler()

Line2.GetBindingExpression(Polyline.PointsProperty).UpdateTarget(); 

を追加することであるかもしれません

関連する問題