如何在WPF中将多边形绑定到已有的点集合?

3

我的当前实现在表单上没有显示任何内容,尽管我认为已经绑定的集合中有数据(我在调试中检查过)。

以下是一些代码:

    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代码如下:

<Polygon x:Name="imagePolygon" Points="{Binding ImagePoints}" Stretch="Fill" Fill="Black" Opacity="0.8" />

现在,我通过编写代码完成了所有绑定。在这个示例中,它可以正常工作,但是在我的代码中,点没有显示在多边形上。

有什么建议吗?

编辑:这是完整的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>

编辑:在修改配置文件后,我在输出窗口中发现了这个:

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。 - Paweł Motyl
你在输出窗口中看到任何绑定错误吗?同时尝试调试绑定并查看当PropertyChanged被触发时是否更新。你可以使用DebugConverter(http://www.zagstudio.com/blog/486)来调试绑定。 - decyclone
那么ImagePoints属性定义在哪里?是直接在窗口的代码后台中定义的吗? - Peter Hansen
在窗口的代码中,有一个Points="{Binding ImagePoints}"。我有一个名为imagePoints PointCollection的方法,从表单的构造函数中调用以初始化它。 - EdwinG
1个回答

5

我不确定为什么您会遇到绑定错误,但乍一看您的代码似乎没问题。

我写了一小段代码测试过了,您可以参考一下,看看是否有遗漏的地方。我猜这个代码应该和您的差不多。

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) });
    }
}

谢谢!我发现问题不在于绑定,而是在于设置点。感谢您的帮助! - EdwinG

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接