Polyline使用DataBinding和PointCollection进行连续更新

Kai*_*pka 5 data-binding wpf polyline c#-4.0


首先,我描述了我想实现的目标.我希望可视化连续数据流(每秒最多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; }
    }
Run Code Online (Sandbox Code Playgroud)

我知道这不是一个好的编码风格,但对于我来说,首先测试它已经足够了.我将数组数据用于x值,将data1用于y值.任何人都可以告诉我这个绑定有什么问题吗?每当出现新值时,如何持续更新视图?
感谢您的帮助.

[更新的新版本]视图:


<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"));
    }
Run Code Online (Sandbox Code Playgroud)

现在我想要做的就是摆脱这条线:grid.DataContext = this;这样我可以使用我的MVVM还是有另一种可能性?

小智 7

为了成功地将Polyline Points属性绑定到viewmodel(即在绑定的PointCollection更改时更新它),您应该避免将PointCollection更改为集合(Clear,Add等).Polyline不会注意到,即使使用自定义转换器绑定到ObservableCollection of Points也无济于事.

相反,您应该将PointCollection视为属性:使用新创建的PointCollection设置它,并触发NotifyPropertyChanged事件:

    private PointCollection points = new PointCollection();
    public PointCollection Points 
    {
        get { return points; }
        set
        {
            points = value;
            NotifyPropertyChanged("Points");
        }
    }

    public void SomeUpdateFunc() 
    {
        PointCollection pc = new PointCollection();

        // Do some adding: pc.Add(new Point(x, y)); etc

        this.Points = pc; // set via the setter, so the notification will fire
    }
Run Code Online (Sandbox Code Playgroud)

现在Polyline应该正确更新,祝你好运!


Aar*_*ver 2

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

此外,这里有一篇类似的SO 帖子,其中还涉及一些其他选项,以使 UI 意识到您的更改。