我不确定为什么您会收到绑定错误,但乍一看您的代码看起来不错。
我写了一小段可以运行的代码,所以你可以检查一下,看看是否遗漏了一些东西。我想应该和你的有点相似吧。。
XAML的相关部分:
<TabItem x:Name="imageTab" Header="Full Image" Height="23" VerticalAlignment="Top">
<Border BorderBrush="Black" BorderThickness="1" Margin="10">
<StackPanel>
<Polygon x:Name="imagePolygon" Points="{Binding ImagePoints}" Stretch="Fill" Fill="Black" Opacity="0.8" />
<Button Content="Set new points" Click="btnSetNew" />
</StackPanel>
</Border>
</TabItem>
窗口的隐藏代码:
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) });
}
}