我的視圖中有兩個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;
};
}
}
準確地說。這是'OnPropertyChanged'第二次咬我。 – Jir 2014-11-02 09:56:26
至少有一件事可以幫助我,在屬性設置器中調用「OnPropertyChanged」。這應該有助於減少小錯誤。 – ShyKnee 2014-11-02 19:12:00