如何绑定一个多边形在WPF现有PointCollection?(How do I bind a Po

2019-07-04 04:03发布

我目前的实现并不显示在表格上任何东西,即使我想收藏界有数据(我在调试检查)。

下面是一些代码:

    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')

Answer 1:

我不知道为什么你正在绑定错误,但在第一眼看到你的代码看起来不错。

我已经写了一小段代码,这样的作品,这样你就可以检查出来,看看你是否错过了一些东西。 我想这一定是有点类似于你..

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


文章来源: How do I bind a Polygon to an existing PointCollection in WPF?