Why doesn't this data binding work?

前端 未结 3 1434
说谎
说谎 2021-01-13 20:44

I have a ViewModel class that contains a list of points, and I am trying to bind it to a Polyline. The Polyline picks up the initial list of points, but does not notice when

相关标签:
3条回答
  • 2021-01-13 21:00

    Change your PointCollections Property to a dependency property:

    public PointCollection Pts
            {
                get { return (PointCollection)GetValue(PtsProperty); }
                set { SetValue(PtsProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Pts.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty PtsProperty =
                DependencyProperty.Register("Pts", typeof(PointCollection), typeof(ViewModel), new UIPropertyMetadata(new PointCollection()));
    

    BTW Doing this, you won't need to fire the PropertyChanged event.

    Oh sorry, and your object needs to inherit from DependencyObject

        public class ViewModel : DependencyObject 
    { 
    //... 
    }
    
    0 讨论(0)
  • 2021-01-13 21:04

    i got it to work as a POCO by implementing INotifyCollectionChanged instead of INotifyPropertyChanged.

    0 讨论(0)
  • 2021-01-13 21:22

    It is quite likely that since it is binding to the collection, it will need something like ObservableCollection<T>. What happens if you switch from PointCollection to ObservableCollection<Point>?

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