Polyline使用DataBinding和PointCollection进行连续更新 [英] Polyline using DataBinding and PointCollection for continuous update

查看:519
本文介绍了Polyline使用DataBinding和PointCollection进行连续更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先我描述我的目标,我想要achive。我想要可视化一个连续的数据流(每秒最多1000个值,但可以减少)。这个数据流应该可视化为图表 - 更精确的是它是ECG的可视化。
我的第一个想法是使用折线并将其绑定到点集合。这里的问题是UI上没有显示任何内容。也许这是一个错误的aproach这个任务。欢迎更好的想法。这里ist我的代码到目前为止。第一个视图:

  
< Canvas>
< Polyline Points ={Binding Points}Stroke =RedStrokeThickness =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 {
Points.Add(new Point(data [i],data1 [i]))
}
}

private PointCollection _points = new PointCollection();
public PointCollection Points
{
get {return _points; }
}

}


我知道这是没有好的编码风格,但首先测试它足够为我。我使用数组数据的x值和数据1的y值。任何人都可以告诉我这个绑定有什么问题?每当新值出现时,为视图的连续更新应该做什么?

感谢您的帮助。



[更新的新版本]
视图:

  
< Window.Resources>
< my:PointCollectionConverter x:Key =myPointsConverter/>
< /Window.Resources>
< Grid Name =grid>
< Polyline x:Name =ekglineIPoints ={Binding Points,Converter = {StaticResource myPointsConverter}}Stroke =RedStrokeThickness =2/>
< Button Content =ButtonClick =button1_Click/>
< / Grid>


代码隐藏在启动时绘制折线,

  
public partial MainWindow:Window,INotifyPropertyChanged
{
private short [] data = new short [] { 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 {
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)
{
(int i = 0; i {
Points.Add(new Point(data2 [i],data1 [i]))
}
PropertyChanged(this,new PropertyChangedEventArgs(Points));
}

现在我想做的是摆脱这行: grid.DataContext = this; ,这样我可以使用我的MVVM或有另一种可能性?

解决方案

Kai要使更改通知传播到您的绑定,你应该使用实现变化通知的集合, PointCollection 不这样做。您可以创建自己的集合,但我建议使用 ObservableCollection< T>



是类似的 SO帖子,其中还涉及了一些其他选项,让UI了解您的更改。



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:

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

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

}

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:


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

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

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?

解决方案

Kai to make the change notification propagate to your bindings you should be making use of a collection which implements change notificaiton, PointCollection does not do this. You could create your own collection however I'd recommend making use of ObservableCollection<T>.

In addition here is a similar SO post which also touches on a few other options for making the UI aware of your changes.

这篇关于Polyline使用DataBinding和PointCollection进行连续更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆