如何将多边形绑定到WPF中的现有PointCollection?

即使我认为有限的集合有数据(我在调试中检查过),我当前的实现也没有在表单上显示任何内容。

这是一些代码:

public event PropertyChangedEventHandler PropertyChanged; PointCollection imagePoints; public PointCollection ImagePoints { get { return this.imagePoints; } set { if (this.imagePoints != value) { this.imagePoints = value; if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ImagePoints")); } } } } 

和相应的xaml:

  

现在,我通过编写代码完成了所有绑定。 在这个例子中 ,它工作正常,但在我的情况下,点不会出现在多边形上。

有智慧的珍珠吗?

编辑:这是完整的xaml代码

                         

编辑:修改配置文件后,我在输出窗口中找到它:

 System.Windows.Data Information: 41 : BindingExpression path error: 'ImagePoints' property not found for 'object' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=ImagePoints; DataItem=null; target element is 'Polygon' (Name='imagePolygon'); target property is 'Points' (type 'PointCollection') System.Windows.Data Information: 20 : BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=ImagePoints; DataItem=null; target element is 'Polygon' (Name='imagePolygon'); target property is 'Points' (type 'PointCollection') System.Windows.Data Information: 21 : BindingExpression cannot retrieve value from null data item. This could happen when binding is detached or when binding to a Nullable type that has no value. BindingExpression:Path=ImagePoints; DataItem=null; target element is 'Polygon' (Name='imagePolygon'); target property is 'Points' (type 'PointCollection') System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ImagePoints; DataItem=null; target element is 'Polygon' (Name='imagePolygon'); target property is 'Points' (type 'PointCollection') 

我不确定你为什么会遇到绑定错误,但是第一眼看到的代码看起来很好。

我写了一小段代码,所以你可以检查一下,看看你是否错过了什么。 我猜它一定和你的有点相似..

XAML的相关部分:

         

窗口的代码隐藏:

 public partial class Window1 : Window, INotifyPropertyChanged { public Window1() { InitializeComponent(); this.ImagePoints = new PointCollection (new [] { new Point(1, 2), new Point(34, 12), new Point(12, 99) }); //Important - maybe you missed this? this.DataContext = this; } public event PropertyChangedEventHandler PropertyChanged; PointCollection imagePoints; public PointCollection ImagePoints { get { return this.imagePoints; } set { if (this.imagePoints != value) { this.imagePoints = value; if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("ImagePoints")); } } } } private void btnSetNew(object sender, RoutedEventArgs e) { this.ImagePoints = new PointCollection( new[] { new Point(23, 2), new Point(12, 556), new Point(4, 89) }); } }