first of all I describe my objective I want to achive. I want to visualise a continuous data stream (maximum 1000 values per second but could be reduced). This data stream should be visualised as a chart - being more precise it's a visualisation of an ECG among other things.
My first idea was using polyline and bind it to a point collection. The problem here is that nothing is shown on the UI. Perhaps it's a wrong aproach for this task. Better ideas are welcomed. Here ist my code so far. First the View:
<code><Canvas></code>
<code><Polyline Points="{Binding Points}" Stroke="Red" StrokeThickness="2" /></code>
<code></Canvas></code>
For the sake of simplicity I use the code-behind even though I use the MVVM-pattern. That's also the reason why I want to use the binding and not just the name of the polyline and add the values.
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值,data1作为y值。有人能告诉我这个绑定有什么问题吗?在出现新值时如何连续更新视图?
提前感谢您的帮助。
[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 };
public MainWindow() { InitializeComponent(); // 将当前窗口作为 DataContext 分配给网格 grid.DataContext = this; for (int i = 0; i < data.Length; i++) { // 添加数据点 Points.Add(new Point(data[i], data1[i])); } } // 定义属性更改事件 public event PropertyChangedEventHandler PropertyChanged; // 创建一个可观察的集合 Points 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模式,还有其他可能性吗?
1[System.Windows.Point]”和“System.Windows.Media.PointCollection”。请考虑使用Binding的Converter属性。BindingExpression:Path =; DataItem ='ObservableCollection
1'(HashCode = 45943265);目标元素为'Polyline'(Name ='pline');目标属性为'Points'(类型为'PointCollection') - Kai Krupka