首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重写DataGrid MouseLeftButtonUp绑定到MVVM?

如何重写DataGrid MouseLeftButtonUp绑定到MVVM?
EN

Stack Overflow用户
提问于 2014-12-21 03:43:17
回答 1查看 1.2K关注 0票数 1

我有一个工作的MouseLeftButtonUp绑定,我是从View.cs工作的,但是不能从Viewmodel.cs获得工作。

XAML:

代码语言:javascript
复制
 <DataGrid x:Name="PersonDataGrid" AutoGenerateColumns="False" 
     SelectionMode="Single" SelectionUnit ="FullRow"  ItemsSource="{Binding Person}"
     SelectedItem="{Binding SelectedPerson}" 
     MouseLeftButtonUp="{Binding PersonDataGrid_CellClicked}" >

View.cs:

代码语言:javascript
复制
    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。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-21 08:46:39

使用混合System.Windows.Interactivity程序集不会违反任何MVVM原则,只要VM中定义的命令中没有使用与视图直接相关的逻辑,在这里如何将其用于MouseLeftButtonUp事件:

代码语言:javascript
复制
<DataGrid>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseLeftButtonUp" >
                <i:InvokeCommandAction
                      Command="{Binding MouseLeftButtonUpCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </DataGrid>

在ViewModel中定义MouseLeftButtonUpCommand:

代码语言:javascript
复制
private RelayCommand _mouseLeftButtonUpCommand;
    public RelayCommand MouseLeftButtonUpCommand
    {
        get
        {
            return _mouseLeftButtonUpCommand 
                ?? (_mouseLeftButtonUpCommand = new RelayCommand(
                () =>
                {
                    // the handler goes here 
                }));
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27586538

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档