我有一个工作的MouseLeftButtonUp绑定,我是从View.cs工作的,但是不能从Viewmodel.cs获得工作。
XAML:
<DataGrid x:Name="PersonDataGrid" AutoGenerateColumns="False"
SelectionMode="Single" SelectionUnit ="FullRow" ItemsSource="{Binding Person}"
SelectedItem="{Binding SelectedPerson}"
MouseLeftButtonUp="{Binding PersonDataGrid_CellClicked}" >View.cs:
private void PersonDataGrid_CellClicked(object sender, MouseButtonEventArgs e)
{
if (SelectedPerson == null)
return;
this.NavigationService.Navigate(new PersonProfile(SelectedPerson));
}PersonDataGrid_CellClicked方法将无法从ViewModel.cs中工作。我试过阅读有关混合System.Windows.Interactivity的内容,但没有尝试过,因为在我还在学习MVVM的时候,我想避免使用它。
我尝试过DependencyProperty和RelativeSource绑定,但无法让PersonDataGrid_CellClicked导航到PersonProfile UserControl。
发布于 2014-12-21 08:46:39
使用混合System.Windows.Interactivity程序集不会违反任何MVVM原则,只要VM中定义的命令中没有使用与视图直接相关的逻辑,在这里如何将其用于MouseLeftButtonUp事件:
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp" >
<i:InvokeCommandAction
Command="{Binding MouseLeftButtonUpCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>在ViewModel中定义MouseLeftButtonUpCommand:
private RelayCommand _mouseLeftButtonUpCommand;
public RelayCommand MouseLeftButtonUpCommand
{
get
{
return _mouseLeftButtonUpCommand
?? (_mouseLeftButtonUpCommand = new RelayCommand(
() =>
{
// the handler goes here
}));
}
}https://stackoverflow.com/questions/27586538
复制相似问题