折线的绑定。我究竟做错了什么?

时间:2011-04-17 22:15:17

标签: c# wpf data-binding polyline

我正在尝试我认为简单的事情:绘制一系列点的线条。 如果我将列表静态地放在窗口的xaml中,一切都OK。 如果我执行绑定,则不会显示任何内容。

窗口代码:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Polyline Stretch="Fill" Grid.Column="0" Name="polyline" Stroke="Red" DataContext="{Binding Points}">
    </Polyline>   
</Grid>
public partial class testWindow2 : Window
{
    AudioSignalModelView audioSignalModelView;

    public testWindow2()
    {
        InitializeComponent();

        audioSignalModelView = new AudioSignalModelView();

        this.DataContext = audioSignalModelView;
    }
}

public class AudioSignalModelView
{
    public AudioSignalModelView()
    {
        Point pointA = new Point {X=0,Y=0};
        Point pointB = new Point { X = 0.2, Y = 0.4 };
        Point pointC = new Point { X = 0.8, Y = 0.1 };
        Point pointD = new Point { X = 1, Y = 1 };

        Points.Add(pointA);
        Points.Add(pointB);
        Points.Add(pointC);
        Points.Add(pointD);
    }

    private AudioSignalTest audioSignalTest;

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

我认为绑定是以某种方式完成的,因为如果我在Points属性的getter中放置一个断点,它就会被系统调用...

我的代码显然有什么问题?

1 个答案:

答案 0 :(得分:2)

您希望绑定到Points属性而不是DataContext

<Polyline Stretch="Fill" Grid.Column="0"
          Name="polyline" Stroke="Red"
          Points="{Binding Points}">  <-- Here
</Polyline>   

MSDN page