2012-07-07 1 views
0

개체 모음이 포함 된 사용자 지정 WPF UserControl이 있습니다.itemsource가 업데이트되는 동안 WPF usercontrol에서 항목 컬렉션 업데이트

public class MyUserControl : UserControl 
{ 
    public readonly static DependencyProperty PointsSourceProperty = 
    DependencyProperty.Register("PointsSource", typeof(IEnumerable), typeof(MyUserControl), new FrameworkPropertyMetadata(null, OnPointsSourceChanged)); 

    public IEnumerable PointsSource 
    { 
     get { return GetValue(PointsSourceProperty) as IEnumerable; } 
     set { SetValue(PointsSourceProperty, value); } 
    } 

    private ObservableCollection<DataPoint> _points = new ObservableCollection<DataPoint>(); 
    public ObservableCollection<DataPoint> Points 
    { 
     get { return points; } 
    } 

    private static void OnPointsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     // Expect to update Points collection 
    } 

}

public class DataPoint : DependencyObject 
{ 
    public readonly static DependencyProperty TimeProperty = 
    DependencyProperty.Register("Time", typeof(DateTime), typeof(DataPoint)); 

    public readonly static DependencyProperty ValueProperty = 
    DependencyProperty.Register("Value", typeof(double), typeof(DataPoint)); 

    public DateTime Time 
    { 
     get { return (DateTime)GetValue(DateTimeProperty); } 
     set { SetValue(DateTimeProperty, value); } 
    } 

    public double Value 
    { 
     get { return (double)GetValue(ValueProperty); } 
     set { SetValue(ValueProperty, value); } 
    } 
} 

나는 데이터 뷰 모델에서 관찰 모음입니다 같은 내 컨트롤 정의 :

<my:myUserControl PointsSource="{Binding Data}"> 
<my:myUserControl.Points> 
    <my:Point Time="{Binding TimeUtc}" Value="{Binding Value}" /> 
</my:myUserControl.Points> 
</my:myUserControl> 

내가 PointsSource 동안 포인트 수집을 업데이트 할 수있는 방법을 값이 변경 되었습니까?

답변

0

이 시도 :

private static void OnPointsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    MyUserControl control = d as MyUserControl; 

    // you have to replace ViewModelItemClass with the name of your class T 
    // in ObservableCollection<T> from the property Data in your ViewModel 
    var sourceCollection = e.NewValue as IEnumerable<ViewModelItemClass>; 

    control._points.Clear(); 
    foreach (var item in sourceCollection) 
    { 
     control._points.Add(new DataPoint { Time = item.TimeUtc, Value = item.Value }); 
    } 
} 
관련 문제