6


まず、達成したい目標を説明します。連続データ ストリームを視覚化したいと考えています (1 秒あたり最大 1000 値ですが、減らすことができます)。このデータ ストリームはグラフとして視覚化する必要があります。より正確には、ECG の視覚化です。私の最初のアイデアは、ポリラインを使用してポイント コレクションにバインドすることでした。ここでの問題は、UI に何も表示されないことです。おそらく、このタスクのアプローチは間違っています。より良いアイデアを歓迎します。これまでのところ、私のコードは次のとおりです。最初のビュー:

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

簡単にするために、MVVM パターンを使用していますが、コード ビハインドを使用しています。それが、ポリラインの名前だけでなくバインディングを使用して値を追加したい理由でもあります。


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 };

    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; }
    }

}

私はそれが良いコーディングスタイルではないことを知っていますが、最初のテストでは十分です。x 値には配列データを使用し、y 値には data1 を使用します。そのバインディングの何が問題なのか誰か教えてもらえますか? 新しい値が発生するたびに、ビューを継続的に更新するにはどうすればよいでしょうか?
事前にご協力いただきありがとうございます。

[新しいバージョンを更新] ビュー:


<Window.Resources>
        <my:PointCollectionConverter x:Key="myPointsConverter"/>
</Window.Resources>
    <Grid Name="grid">
        <Polyline x:Name="ekglineI" Points="{Binding Points, Converter={StaticResource myPointsConverter}}" Stroke="Red" StrokeThickness="2"  />
        <Button Content="Button" Click="button1_Click" />
</Grid>
起動時と後でボタンがクリックされたときにポリラインを描画するコード ビハインド。

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 };

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; } }

    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"));
    }

今、私がやりたいことは、この行を取り除くことです: grid.DataContext = this;MVVM を使用できるようにしますか、それとも別の可能性がありますか?

4

3 に答える 3

2

削除する方法が少なくとも 1 つあります。grid.DataContext = this;

Binding to RelativeSourceをグリッド自体に追加します。この場合、xaml ファイルは次のようになります。

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication2">
<Grid Name="grid" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}}">
    <Canvas>
        <Polyline Points="{Binding Points}" Stroke="Red" StrokeThickness="2" />
    </Canvas>
</Grid>

そして、コードビハインドは次のようになります

 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Linq;
 using System.Text;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
 using System.Windows.Documents;
 using System.Windows.Input;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
 using System.ComponentModel;

 namespace WpfApplication2
 {
    public partial class MainWindow : Window , INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();

            for (int i = 0; i < data.Length; i++)
            {
                 Points.Add(new Point(data[i], data1[i]));
            }
            NotifyPropertyChanged("Points");                
        }

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        public PointCollection Points { get { return _points; } }

        public event PropertyChangedEventHandler PropertyChanged;
        private PointCollection _points = new PointCollection();
        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 };


    }
}
于 2012-11-06T14:11:05.867 に答える
2

Kai は、変更通知を実装するコレクションを使用する必要があるバインディングに変更通知を伝達しますが、PointCollectionこれは行いません。独自のコレクションを作成することもできますが、ObservableCollection<T>.

さらに、UI に変更を認識させるための他のいくつかのオプションにも触れている同様のSO 投稿があります。

于 2010-10-18T14:30:46.800 に答える