2013-01-02 180 views
0

我試着綁定一個datagrid的selectedItem與MVVM中的一個屬性。 問題是它不會觸發屬性的「設置」。wpf dataGrid綁定selecteditem不工作

在XAML部分

我有:

<WPFCtrlDg:ExtDataGrid Name="_edgMessage" Grid.Row="1" 
     ItemsSource="{Binding Path=LNE_MESSAGE, Mode=OneWay}" 
     SelectedItem="{Binding Path=CurrentMessage, Mode=TwoWay}"> 
代碼部分

private LNE_MESSAGE _currentMessage; 
    public LNE_MESSAGE CurrentMessage 
    { 
     get 
     { 
      if (_currentMessage == null) 
      { 
       ICollectionView collectionView = CollectionViewSource.GetDefaultView(LNE_MESSAGE); 
       if (collectionView != null) 
        return collectionView.CurrentItem as LNE_MESSAGE; 
       return null; 
      } 
      return _currentMessage; 
     } 
     set 
     { 
      ICollectionView collectionView = CollectionViewSource.GetDefaultView(LNE_MESSAGE); 
      if (collectionView != null) 
       collectionView.MoveCurrentTo(value); 

      _currentMessage = value; 
      OnPropertyChanged(() => CurrentMessage); 
     } 
    } 

的extdatagrid是一個自定義的控制和選擇的項目屬性進行這樣:

 public object SelectedItem 
    { 
     get { return GetValue(SelectedItemProperty); } 
     set { SetValue(SelectedItemProperty, value); } 
    } 

    public static readonly DependencyProperty SelectedItemProperty = 
     DependencyProperty.Register("SelectedItem", typeof(object), typeof(ExtDataGrid), 
           new UIPropertyMetadata((s, e) => 
                   { 
                    ExtDataGrid extDg = s as ExtDataGrid; 
                    Debug.Assert(extDg != null); 
                    extDg.CurrentItem = e.NewValue; 
                   })); 

任何想法如何正確綁定selecteditem屬性?

回答

0

檢查您的datacontext是否設置正確。 如果datacontext實際上是使用可視幫助程序設置的,那麼調試並查看locals窗口可以看到。另請參閱輸出窗口以瞭解任何綁定錯誤。

乍一看,您的依賴屬性看起來正確。

+0

您可以在調試時看到您的參數的本地窗口請參閱http://blogs.msdn.com/b/zainnab/archive/2010/01/29/using-the-wpf-tree-visualizer- vstipdebug0004.aspx – Martijn

+0

這兩個窗口empy(不知道我是否需要打印輸出的東西)。順便說一句datagrid加載它的內容connrectly與LME_MESSAGE屬性是在相同的viewmodel比CurrentMessage,所以我認爲datacontext是正確的 – andrea

+0

好吧我現在得到它...我會調試,看到..謝謝 – andrea