如何将多边形绑定到 WPF 中的现有 PointCollection?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13569794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I bind a Polygon to an existing PointCollection in WPF?
提问by EdwinG
My current implementation doesn't show anything on the form even though the collections I thought bounded have data (I checked in debug).
即使我认为有界的集合有数据(我在调试中检查),我当前的实现也没有在表单上显示任何内容。
Here's some code:
这是一些代码:
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"));
}
}
}
}
and the corresponding xaml:
和相应的xaml:
<Polygon x:Name="imagePolygon" Points="{Binding ImagePoints}" Stretch="Fill" Fill="Black" Opacity="0.8" />
Now, I did all the binding by writing the code. In this example, it works just fine, yet in mine, the points don't show up on the polygon.
现在,我通过编写代码完成了所有绑定。在这个例子中,它工作得很好,但在我的例子中,点没有出现在多边形上。
Any pearls of wisdom?
有智慧的珍珠吗?
Edit:here's the complete xaml code
编辑:这是完整的 xaml 代码
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication2.HistogramWindow"
Title="HistogramWindow" Height="436" Width="604">
<Grid>
<TabControl HorizontalAlignment="Left" Height="406" VerticalAlignment="Top" Width="596" >
<TabItem x:Name="imageTab" Header="Full Image" Height="23" VerticalAlignment="Top">
<Border BorderBrush="Black" BorderThickness="1" Margin="10">
<Polygon x:Name="imagePolygon" Points="{Binding ImagePoints}" Stretch="Fill" Fill="Black" Opacity="0.8" />
</Border>
</TabItem>
<TabItem x:Name="boneTab" Header="Bone">
<Border BorderBrush="Black" BorderThickness="1" Margin="10">
<Border.BindingGroup>
<BindingGroup/>
</Border.BindingGroup>
<Polygon x:Name="bonePolygon" Points="{Binding BonePoints}" Stretch="Fill" Fill="Black" Opacity="0.8" >
</Polygon>
</Border>
</TabItem>
<TabItem x:Name="fatTab" Header="Fat" HorizontalAlignment="Left" Height="20" VerticalAlignment="Top" Width="57">
<Border BorderBrush="Black" BorderThickness="1" Margin="10">
<Polygon x:Name="fatPolygon" Points="{Binding FatPoints}" Stretch="Fill" Fill="Black" Opacity="0.8" />
</Border>
</TabItem>
</TabControl>
</Grid>
Edit: After a modification of the config file, I find this in the output window:
编辑:修改配置文件后,我在输出窗口中找到了这个:
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')
回答by Peter Hansen
I'm not sure why you are getting binding errors, but on first sight the code you have looks fine.
我不确定为什么会出现绑定错误,但乍一看,您拥有的代码看起来不错。
I have written a small piece of code that works, so you can check that out and see if you have missed something. I guess it must be kinda similar to yours..
我写了一小段有效的代码,所以你可以检查一下,看看你是否遗漏了什么。我想它应该和你的有点相似..
Relevant part of the XAML:
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>
Code-behind for the window:
窗口的代码隐藏:
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) });
}
}

