2013-02-13 98 views
0

我有一些嵌套集合的ObjectmodelWPF數據綁定與嵌套集合不更新控制

public class MainViewModel : BaseViewModel 
{ 
    private Dictionary<String, Diagram> _diagrams; 

    public Dictionary<String, Diagram> Diagrams 
    { 
     get { return _diagrams; } 
     set 
     { 
      _diagrams = value; 
      OnPropertyChanged("Diagrams"); 
     } 
    } 

}

基本視圖模型實現INotifyPropertyChanged。裏面Diagram有曲線的集合:

public class Diagram 
{ 
    private ObservableCollection<DiagramCurve> curves; 
    [JsonIgnoreAttribute] 
    public ObservableCollection<DiagramCurve> Curves 
    { 
     get { return curves; } 
    } 

    public Diagram() 
    { 
     curves = new ObservableCollection<DiagramCurve>(); 
    } 
} 

我綁定的DiagramCanvas一個實例在我的XAML中這樣的:

<wd:DiagramCanvas x:Name="diagramCanvas" 
          Diagram ="{Binding Diagrams[Diagram1]}" 
          Grid.Row="2" Grid.Column="1" 
          Height="auto" Width="Auto" 
          HorizontalAlignment="Stretch" 
          VerticalAlignment="Stretch" 
          Margin="10" 
          Background="{StaticResource DiagrambackgroundBrush }" /> 

,如果我完全分配的MainView一個的圖表屬性,它工作正常新的圖集合。但是我需要的是當曲線集合改變時DiagramCanvas控件得到更新。但是這並沒有發生。

回答

0

我試圖從一個「的ObservableCollection」繼承Diagram,但即使這樣也沒有幫助,如ObservableCollection dependency property does not update when item in collection is deleted解釋。

正如我CustomControl DiagramCanvas的的DependencyProperty結合詞典MainViewModel.Diagrams的值項不是一個Diagram實例的屬性中,DiagramCanvas的的DependencyProperty沒有OnPropertyChanged事件被觸發,當Diagram實例變化的屬性。

只有當一個新的值被分配到值項的字典裏面MainViewModel.Diagrams,這一事件被炒魷魚,這東陽分配一個新值的DiagramCanvasDependencyProperty

因此我必須手動訂閱集合的事件:。

public class DiagramCanvas : Canvas 
{ 
    // Is called only when new Items get inserted info the Dictionary 
    static void OnDiagramPropertyChanged(DependencyObject obj, 
            DependencyPropertyChangedEventArgs args) 
    { 
     DiagramCanvas THIS = (DiagramCanvas)obj; 

     Diagram new_diagram = (Diagram)args.NewValue; 
     Diagram old_diagram = (Diagram)args.NewValue; 

     if (old_diagram != null) 
     { 
     // Unsubscribe from CollectionChanged on the old collection 
     old_diagram.Curves.CollectionChanged -= THIS.OnCollectionChanged; 
     } 

     if (new_diagram != null) 
     { 
     // Subscribe to CollectionChanged on the new collection 
     new_diagram.Curves.CollectionChanged += THIS.OnCollectionChanged; 
     } 
    } 

    //Is called everytime the Curves collecion changes 
    void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 

    } 
} 
0

您的綁定位於MainViewModelDiagrams屬性中,並選擇對應於「Diagram1」字符串的圖。在綁定路徑中,沒有提及Curves屬性,因此當曲線引發CollectionChanged事件時,綁定不會更新。爲了防止這種情況發生,您可以在MainViewModel中註冊此活動,並在您的字典中出現PropertyChanged事件。

但是......如果我沒有弄錯,.Net Dictionary沒有實現INotifyCollectionChanged,我不確定綁定是否會更新。你應該儘量讓我知道。

編碼快樂,

安東尼

+0

我該如何在'MainViewModel'中註冊事件?我已經爲圖集合使用了一個ObservableDictionary類,但這不會有幫助,因爲我不更改此集合。 – Thomas 2013-02-13 21:27:56

+0

是的,在這種情況下,您不應該註冊到Diagrams Collection更改的事件,而是註冊到Curves Collection更改的事件。第二個想法是:由於您也創建了自己的控件DiagramCanvas,因此該控件可以在收到圖表時註冊到Curvers.CollectionChanged。如果圖表發生變化,或者卸載視圖以避免內存泄漏,請不要忘記註銷事件。您也可以更改您的DiagramCanvas以提供Curves DependencyProperty,而不是圖。這樣,所有的事件註冊將是自動的。 – 2013-02-14 21:47:48

+0

實際上,您可以在我自己的答案中看到DiagramCanvas現在註冊到Curves.OnColletionChanged事件。我需要綁定到DiagramProperty,因爲我的Control需要更多來自Diagram的數據作爲曲線。 – Thomas 2013-02-15 07:38:26