Why doesn't this data binding work?

前端 未结 3 1460
说谎
说谎 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 
    { 
    //... 
    }
    

提交回复
热议问题