How do I bind a Polygon to an existing PointCollection in WPF?

前端 未结 1 1398
轻奢々
轻奢々 2021-01-06 12:53

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:

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-06 13:17

    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:

    
        
            
                
                

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

    0 讨论(0)
提交回复
热议问题