2013-01-02 184 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