C# WPF-可以将命令绑定到一个";SelectedItem";在DataGrid中,但无法调用它
我的视图中有两个C# WPF-可以将命令绑定到一个";SelectedItem";在DataGrid中,但无法调用它,c#,wpf,xaml,mvvm,datagrid,C#,Wpf,Xaml,Mvvm,Datagrid,我的视图中有两个DataGrids。第一个在调用ViewModel构造函数时填充。当用户单击第一个数据网格中的一行时,将填充第二个数据网格。 我认为问题在于,当我运行绑定到第一个数据网格(rowclickcommand)的操作时,我为第二个数据网格(rowclickcommand)设置了操作 同样,我也看不出为什么当我点击第二个数据网格中的一个项目时它没有被调用 我的方法有什么问题 下面是.xaml文件: <Grid Grid.Row="0"> <DataGrid
DataGrid
s。第一个在调用ViewModel构造函数时填充。当用户单击第一个数据网格中的一行时,将填充第二个数据网格。
我认为问题在于,当我运行绑定到第一个数据网格(rowclickcommand
)的操作时,我为第二个数据网格(rowclickcommand
)设置了操作
同样,我也看不出为什么当我点击第二个数据网格中的一个项目时它没有被调用
我的方法有什么问题
下面是.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>
ViewModel的定义如下:
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;
};
}
}
<i:InvokeCommandAction
Command="{Binding RowAClickCommand,
RelativeSource={RelativeSource Mode=FindAncestor
AncestorType={x:Type Window}}}"/>
public类MainWindowViewModel:INotifyPropertyChanged
{
public ICommand RowAClickCommand{get{return\u RowAClickCommand;}}
公共ICommand RowBClickCommand{get{return}
// ...
公共主窗口视图模型()
{
_rowAClickCommand=新建主窗口。DelegateCommand(()=>
{
_rowBClickCommand=新建主窗口。DelegateCommand(()=>
{
MessageBox.Show(“x”);
});
var complexTypeB=新的complexTypeB();
_b数据=新的可观察收集(complexTypeB.l);
不动产变更(“BData”);
BView=CollectionViewSource.GetDefaultView(_Bdata);
BView=CollectionViewSource.GetDefaultView(_Bdata);
BView.CurrentChanged+=委托
{
_currSelectedB=(B)BView.CurrentItem;
};
});
var someComplexTypeInstance=new ComplexTypeA();
_Adata=新的ObservableCollection(someComplexTypeInstance.l);
AView=CollectionViewSource.GetDefaultView(_Adata);
AView.CurrentChanged+=委托
{
_currSelectedA=(A)AView.CurrentItem;
};
}
}
我认为您必须更改命令的绑定,如下所示:
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;
};
}
}
<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}}}"/>
您需要在
\u RowBClickCommand
设置为其他内容后调用OnPropertyChanged(“RowBClickCommand”)
。大概是这样的:
_rowAClickCommand = new DelegateCommand(() =>
{
MessageBox.Show("ACalled");
_rowBClickCommand = new DelegateCommand(() =>
{
MessageBox.Show("BCalled");
});
OnPropertyChanged("RowBClickCommand");
});
尝试过,但不幸的是没有解决问题。问题是,根据Resharper的说法,
RowAClickCommand
和RowBClickCommand
似乎绑定正确。@Jir你能发布一个示例项目吗?准确地说。这是OnPropertyChanged
第二次咬我。至少有一件事对我有帮助,那就是在属性设置器中调用OnPropertyChanged
。这将有助于减少小错误。