Polyline mit DataBinding und PointCollection zur kontinuierlichen Aktualisierung


Zunächst beschreibe ich mein Ziel, das ich erreichen möchte. Ich möchte einen kontinuierlichen Datenstrom visualisieren (maximal 1000 Werte pro Sekunde, könnte aber reduziert werden). Dieser Datenstrom sollte als Diagramm dargestellt werden - genauer gesagt ist es unter anderem eine Darstellung eines EKGs. Meine erste Idee war, Polylinie zu verwenden und an eine Punktesammlung zu binden. Das Problem hierbei ist, dass auf der Benutzeroberfläche nichts angezeigt wird. Vielleicht ist es ein falscher Ansatz für diese Aufgabe. Bessere Ideen sind willkommen. Hier ist mein Code soweit. Zuerst die Ansicht:

 
<code><Canvas></code>
  <code><Polyline Points="{Binding Points}" Stroke="Red" StrokeThickness="2" /></code>
<code></Canvas></code>

Der Einfachheit halber benutze ich den Code-Behind, obwohl ich das MVVM-Pattern verwende. Das ist auch der Grund, warum ich die Bindung und nicht nur den Namen der Polylinie verwenden und die Werte hinzufügen möchte.


public partial class MainWindow : Window
{
   private short[] data = new short[]{ 10,30,50,70,90,110,130,150,170,190,210 };
   private short[] data1 = new short[] { 15,14,16,13,17,12,18,11,19,10,24 };<p></p>

<pre><code>    public MainWindow()
    {
        InitializeComponent();
        for (int i = 0; i < data.Length; i++)
        {
            Points.Add(new Point(data[i], data1[i]));
        }
    }

    private PointCollection _points = new PointCollection();
    public PointCollection Points
    {
        get { return _points; }
    }
</code></pre>

} I know that is no good coding style but for first tests its enough for me. I use array data for x-values and data1 for y-values. Can anyone tell me whats wrong with that binding? What's to be done for a continuous update of the view, whenever new values occur?
Thanks for your help in advance.

[Updated new version] The view:


<code><Window.Resources></code>
        <code><my:PointCollectionConverter x:Key="myPointsConverter"/></code>
<code></Window.Resources></code>
    <code><Grid Name="grid"></code>
        <code><Polyline x:Name="ekglineI" Points="{Binding Points, Converter={StaticResource myPointsConverter}}" Stroke="Red" StrokeThickness="2"  /></code>
        <code><Button Content="Button" Click="button1_Click" /></code>
<code></Grid></code>
The code-behind which draws a polyline on startup and later on when a button is clicked.

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private short[] data = new short[] { 10, 30, 50, 70, 90, 110, 130, 150, 170, 190, 210 };
        private short[] data2 = new short[] { 230, 250, 270, 290, 300, 310, 330, 350, 370, 390, 410 };
        private short[] data1 = new short[] { 15, 14, 16, 13, 17, 12, 18, 11, 19, 10, 24 };<p></p>

<p>public MainWindow()
        {
            InitializeComponent();
            grid.DataContext = this;
            for (int i = 0; i < data.Length; i++)
            {
                Points.Add(new Point(data[i], data1[i]));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection _points = new ObservableCollection();
        public ObservableCollection Points
        {
            get { return _points; }
        }</p>

<pre><code>    private void button1_Click(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < data2.Length; i++)
        {
            Points.Add(new Point(data2[i], data1[i]));
        }
        PropertyChanged(this, new PropertyChangedEventArgs("Points"));
    }
</code></pre>

Now what I want to do is getting rid of this line: grid.DataContext = this; so that I can use my MVVM or is there another possibility?

Antworten auf die Frage(6)

Ihre Antwort auf die Frage