如何绑定一个多边形在WPF现有PointCollection? [英] How do I bind a Polygon to an existing PointCollection in WPF?

查看:220
本文介绍了如何绑定一个多边形在WPF现有PointCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的实现并不表明即使我以为界集合有数据(我在调试选中)表格上的任何东西。



下面是一些代码:

 公共事件PropertyChangedEventHandler的PropertyChanged; 
PointCollection imagePoints;
公共PointCollection ImagePoints
{
得到
{
返回this.imagePoints;
}

{
如果(this.imagePoints =价值!)
{
this.imagePoints =价值;
如果(this.PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(ImagePoints));
}
}
}
}

和相应的XAML:

 <多边形X:NAME =imagePolygon点数={结合ImagePoints}拉伸=填写填充=黑不透明度=0.8/> 

现在,我做了所有的写代码的结合。在此例如,它工作得很好,但在矿山,点并不在多边形显示出来。



智慧珍珠任何



编辑:这里是完整的XAML代码

 <窗​​口
的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/2006MC:可忽略=DX:类=WpfApplication2 .HistogramWindow
标题=HistogramWindowHEIGHT =436WIDTH =604>
<网格和GT;
<的TabControl的Horizo​​ntalAlignment =左HEIGHT =406VerticalAlignment =评出的WIDTH =596>
< TabItem的X:名称=imageTab标题=完整图像HEIGHT =23VerticalAlignment =评出的>
< BORDER = BorderBrush黑了borderThickness =1保证金=10>
<多边形X:NAME =imagePolygon点数={结合ImagePoints}拉伸=填充填充=黑不透明度=0.8/>
< /边框>
< / TabItem的>
< TabItem的X:名称=boneTab标题=骨头>
< BORDER = BorderBrush黑了borderThickness =1保证金=10>
< Border.BindingGroup>
< BindingGroup />
< /Border.BindingGroup>
<多边形X:NAME =bonePolygon点数={结合BonePoints}拉伸=填充填充=黑不透明度=0.8>

< /多边形>
< /边框>
< / TabItem的>
< TabItem的X:名称=fatTab标题=胖的Horizo​​ntalAlignment =左身高=20VerticalAlignment =评出的WIDTH =57>
< BORDER = BorderBrush黑了borderThickness =1保证金=10>
<多边形X:NAME =fatPolygon点数={结合FatPoints}拉伸=填充填充=黑不透明度=0.8/>
< /边框>
< / TabItem的>
< / TabControl的>

< /网格和GT;





编辑:进行修改后的配置文件,我在输出窗口中找到这样的:

  System.Windows.Data信息:41:BindingExpression路径错误: ImagePoints属性未找到对象,因为数据项为null。这可能发生,因为数据提供者并没有产生任何数据。 BindingExpression:路径= ImagePoints;的DataItem = NULL;目标元素是多边形(名称='imagePolygon'); target属性是'点'(类型'PointCollection')
System.Windows.Data信息:20:BindingExpression不能因为缺少信息检索值。 BindingExpression:路径= ImagePoints;的DataItem = NULL;目标元素是多边形(名称='imagePolygon'); target属性是'点'(类型'PointCollection')
System.Windows.Data信息:21:BindingExpression无法检索空数据项值。被分离或绑定到没有任何价值可空类型时,结合时,这可能发生。 BindingExpression:路径= ImagePoints;的DataItem = NULL;目标元素是多边形(名称='imagePolygon'); target属性是'点'(类型'PointCollection')
System.Windows.Data信息:10:使用绑定和没有有效的回退值存在无法检索值;使用默认值来代替。 BindingExpression:路径= ImagePoints;的DataItem = NULL;目标元素是多边形(名称='imagePolygon'); target属性是'点'(类型'PointCollection')


解决方案

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



我已经写了一小片这样的作品,这样你就可以检查出来,看看你错过了什么代码。我想这一定是有点类似你们..



在XAML的有关部分:

 < TabItem的X:名称=imageTab标题=完整图像HEIGHT =23VerticalAlignment =评出的> 
< BORDER = BorderBrush黑了borderThickness =1保证金=10>
<&StackPanel的GT;
<多边形X:NAME =imagePolygon点数={结合ImagePoints}拉伸=填充填充=黑不透明度=0.8/>
<按钮内容=设置新点点击=btnSetNew/>
< / StackPanel的>
< /边框>
< / TabItem的>



代码隐藏的窗口:

 公共部分类窗口1:窗口,INotifyPropertyChanged的
{
公共窗口1()
{
的InitializeComponent();

this.ImagePoints =新PointCollection
(新[] {新点(1,2),新的点(34,12),新的点(12,99)});

//重要 - 也许你错过了这个?
this.DataContext =这一点;
}

公共事件PropertyChangedEventHandler的PropertyChanged;
PointCollection imagePoints;
公共PointCollection ImagePoints
{
得到
{
返回this.imagePoints;
}

{
如果(this.imagePoints =价值!)
{
this.imagePoints =价值;
如果(this.PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(ImagePoints));
}
}
}
}

私人无效btnSetNew(对象发件人,RoutedEventArgs E)
{
this.ImagePoints =新PointCollection(
新的[] {新的点(23,2),新的点(12,556),新点(4,89)});
}
}


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:

<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

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

解决方案

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:

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

这篇关于如何绑定一个多边形在WPF现有PointCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆