6


首先,我描述了我想要实现的目标。我想可视化一个连续的数据流(每秒最多 1000 个值,但可以减少)。这个数据流应该被可视化为一个图表——更准确地说,它是一个心电图的可视化等等。我的第一个想法是使用折线并将其绑定到点集合。这里的问题是 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 值,将 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"));
    }

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

4

3 回答 3

9

为了成功地将折线点属性绑定到您的视图模型(即在绑定的 PointCollection 更改时更新它),您应该避免将 PointCollection 更改为集合(清除、添加等)。折线不会注意到,即使使用自定义转换器绑定到点的 ObservableCollection 也无济于事。

相反,您应该将 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
    }

现在折线应该正确更新了,祝你好运!

于 2012-10-11T20:13:54.870 回答
2

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

此外,这里有一个类似的SO 帖子,其中还涉及一些其他选项,可以让 UI 了解您的更改。

于 2010-10-18T14:30:46.800 回答
2

至少有一种可能的删除方式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 回答