2014-11-01 75 views
0

我的視圖中有兩個DataGrid s。第一個是在調用ViewModel構造函數時填充的。當用戶點擊第一個DataGrid中的一行時,第二個被填充。 我認爲問題是我爲第二個DataGrid設置動作(RowBClickCommand),當我運行綁定到第一個DataGrid(RowAClickCommand)的動作。WPF - 可以將命令綁定到DataGrid中的「SelectedItem」,但不能調用它

再次,我看不到任何理由,當我點擊第二個DataGrid中的項目時,它不會被調用。

我的方法有什麼問題?

這裏的的.xaml文件:

<Grid Grid.Row="0"> 
    <DataGrid 
    x:Name="ADataGrid" 
    SelectedItem="{Binding CurrSelectedA, Mode=TwoWay}" 
    ItemsSource="{Binding AData}"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="MouseLeftButtonUp" > 
       <i:InvokeCommandAction 
       Command="{Binding RowAClickCommand}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </DataGrid> 
</Grid> 

<Grid Grid.Row="1"> 
    <DataGrid 
    x:Name="BDataGrid" 
    SelectedItem="{Binding CurrSelectedB, Mode=TwoWay}" 
    ItemsSource="{Binding BData}"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="MouseLeftButtonUp" > 
       <i:InvokeCommandAction 
       Command="{Binding RowBClickCommand}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
    </DataGrid> 
</Grid> 

視圖模型的定義如下:

public class MainWindowViewModel : INotifyPropertyChanged 
{ 
    public ICommand RowAClickCommand { get { return _rowAClickCommand; } } 
    public ICommand RowBClickCommand { get { return _rowBClickCommand; } } 
    // ... 

    public MainWindowViewModel() 
    { 
     _rowAClickCommand = new MainWindow.DelegateCommand(() => 
     { 
      _rowBClickCommand = new MainWindow.DelegateCommand(() => 
      { 
       MessageBox.Show("x"); 
      }); 

      var complexTypeB = new ComplexTypeB(); 
      _Bdata = new ObservableCollection<B>(complexTypeB.l); 
      OnPropertyChanged("BData"); 

      BView = CollectionViewSource.GetDefaultView(_Bdata); 

      BView = CollectionViewSource.GetDefaultView(_Bdata); 
      BView.CurrentChanged += delegate 
      { 
       _currSelectedB = (B)BView.CurrentItem; 
      }; 
     }); 

     var someComplexTypeInstance = new ComplexTypeA(); 
     _Adata = new ObservableCollection<A>(someComplexTypeInstance.l); 

     AView = CollectionViewSource.GetDefaultView(_Adata); 
     AView.CurrentChanged += delegate 
     { 
      _currSelectedA = (A)AView.CurrentItem; 
     }; 
    } 
} 

回答

1

您需要在_rowBClickCommand設置爲其他內容後致電OnPropertyChanged("RowBClickCommand")。事情是這樣的:

_rowAClickCommand = new DelegateCommand(() => 
{ 
    MessageBox.Show("ACalled"); 
    _rowBClickCommand = new DelegateCommand(() => 
    { 
     MessageBox.Show("BCalled"); 
    }); 

    OnPropertyChanged("RowBClickCommand"); 
}); 
+0

準確地說。這是'OnPropertyChanged'第二次咬我。 – Jir 2014-11-02 09:56:26

+0

至少有一件事可以幫助我,在屬性設置器中調用「OnPropertyChanged」。這應該有助於減少小錯誤。 – ShyKnee 2014-11-02 19:12:00

0

我想你一定如下更改命令的結合:

<i:InvokeCommandAction 
    Command="{Binding RowAClickCommand, 
         RelativeSource={RelativeSource Mode=FindAncestor 
                AncestorType={x:Type Window}}}"/> 

同樣的:

<i:InvokeCommandAction 
    Command="{Binding RowBClickCommand, 
         RelativeSource={RelativeSource Mode=FindAncestor 
                AncestorType={x:Type Window}}}"/> 
+0

嘗試,但它並沒有不幸的是解決問題。根據Resharper的說法,「RowAclickCommand」和「RowBClickCommand」似乎都是正確綁定的。 – Jir 2014-11-01 18:35:51

+0

@Jir你可以發佈一個示例項目嗎? – Vishal 2014-11-01 18:43:56

相關問題