双击后获取行信息

Jos*_*osh 9 .net c# xaml mvvm caliburn.micro

我试图在双击事件后从数据网格中检索行信息.我有事件设置,但现在我只需要设置函数来从行中检索数据.

XAML:

    <DataGrid 
        Width="Auto" 
        SelectionMode="Extended" 
        IsReadOnly="True" 
        Name="ListDataGrid"
        AutoGenerateColumns="False"
        ItemsSource="{Binding ListFieldObject.MoviesList}"
        DataContext="{StaticResource MovieAppViewModel}"
        cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect()]">

        <DataGrid.Columns>
            <DataGridTextColumn Width="200" IsReadOnly="True" Header="Title" Binding="{Binding Title}"/>
            <DataGridTextColumn Width="100" IsReadOnly="True" Header="Rating" Binding="{Binding Rating}"/>
            <DataGridTextColumn Width="100" IsReadOnly="True" Header="Stars" Binding="{Binding Stars}"/>
            <DataGridTextColumn Width="93" IsReadOnly="True" Header="Release Year" Binding="{Binding ReleaseYear}"/>
        </DataGrid.Columns>
    </DataGrid>
Run Code Online (Sandbox Code Playgroud)

C#(MVVM ViewModel):

     public void RowSelect()
     {
         //now how to access the selected row after the double click event?
     }
Run Code Online (Sandbox Code Playgroud)

非常感谢!

Dav*_*iff 22

你也可以这样做:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="cal:Message.Attach" Value="[MouseDoubleClick] = [Action RowSelect($dataContext)]"/>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)

然后

public void RowSelect(MoviesListItem movie)
{
     //now how to access the selected row after the double click event?
}
Run Code Online (Sandbox Code Playgroud)

  • 这是更好的解决方案,然后接受答案,因为它只捕获行的双击而不是标题. (2认同)

Leo*_*Leo 7

使用Caliburn非常简单,只需在XAML上传递$ dataContext:

 cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect($dataContext)]">
Run Code Online (Sandbox Code Playgroud)

并将您的方法更改为:

public void RowSelect(MoviesListItem movie)
{
     //now how to access the selected row after the double click event?
}
Run Code Online (Sandbox Code Playgroud)

//编辑 对不起,上面的解决方案只有当操作在datatemplate本身时才有效...另一个解决方案是使用SelectedItem绑定并在你的方法上使用它:

<DataGrid 
    SelectedItem="{Binding SelectedMovie,Mode=TwoWay}"
    cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect()]">
Run Code Online (Sandbox Code Playgroud)

并在您的代码上:

public void RowSelect()
{
   //SelectedMovie is the item where the user double-cliked
}
Run Code Online (Sandbox Code Playgroud)