双击后获取行信息

11

我正在尝试在双击事件后从数据网格中检索行信息。我已经设置好了事件,但现在我只需要设置函数来从行中检索数据。

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>

C#(MVVM ViewModel):

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

非常感谢!


据我所知,这非常困难,如果不是不可能的话。很多网站都讨论了这个问题,例如 http://www.scottlogic.co.uk/blog/colin/2008/12/wpf-datagrid-detecting-clicked-cell-and-row/ 和 https://dev59.com/w1bUa4cB1Zd3GeqPAaBf。 - Phil
5个回答

27

你也可以选择这样做:

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

那么

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

3
这个解决方案比被接受的答案好得多,因为它只捕捉行上的双击而不是标题上的双击。 - Sevenate
1
这就是我一直在寻找的答案,应该标记为答案。 - Teslo.
这是正确的答案!我还要补充一点,如果你想在DataGrid的DataContext上调用一个方法,你也可以添加以下Setter:<Setter Property="cal:Action.TargetWithoutContext" Value="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}" /> - Shahin Dohan

9
您可以在XAML中直接传递$dataContext:
 cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect($dataContext)]">

并将您的方法更改为:

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

//编辑 抱歉,以上解决方案仅在操作位于数据模板本身时有效...另一种解决方案是绑定SelectedItem并在您的方法中使用它:

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

在您的代码中:

public void RowSelect()
{
   //SelectedMovie is the item where the user double-cliked
}

这就是我最终的做法。感谢帮助! - Josh
这个解决方案是可行的,但从经验来看,如果用户快速点击数据网格的区域,它有不可预测的行为缺陷。最显著的例子:如果用户在滚动大型列表时喜欢快速点击垂直滚动条中的向下箭头或拇指轨迹。许多用户不会点击和拖动滚动条上的拇指,而是猛击箭头或拇指轨迹。由于双击是由数据网格处理而不是数据行处理,因此这种解决方案必然会导致双击执行。 - Adrian
@Josh,这个解决方案也可以处理列标题上的双击,但这并不是必要的。为了防止这种情况,请考虑下面David Kiff的解决方案。 - Sevenate

1

(希望能有所帮助)我不确定你的情况,但这是我在winforms中做的:

            int index = dataGridView2.CurrentRow.Index; //determine which item is selected
            textBox8.Text = dataGridView2.Rows[index].Cells[0].Value.ToString(); //add login

非常感谢您的输入,但我正在使用MVVM模式,不能在代码后台进行调用。我将编辑原始帖子以反映C#位于ViewModel中的事实。 - Josh

1

你可以通过修改DataGrid公开的DataGridRows的控件模板来实现这一点。下面的示例使用WPF和Aero主题。

我所做的唯一事情就是删除了你之前的cal:Message.Attach调用,并将其移动到一个新的“占位符”ContentControl中,该ContentControl包围了“默认”控件模板中的Border(x:Name=DGR_Border)。 (我使用ContentControl是因为它没有自己的可视化内容,并且它公开了一个MouseDoubleClick事件。)

<DataGrid Width="Auto" 
          SelectionMode="Extended" 
          IsReadOnly="True" 
          Name="ListDataGrid"
          AutoGenerateColumns="False"
          ItemsSource="{Binding ListFieldObject.MoviesList}"
          DataContext="{StaticResource MovieAppViewModel}">

    <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.RowStyle>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
        <Setter Property="ValidationErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Foreground="Red" Margin="2,0,0,0" Text="!" VerticalAlignment="Center"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>                                      
                <ControlTemplate TargetType="{x:Type DataGridRow}">
                    <ContentControl cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect($datacontext)]">
                        <Border x:Name="DGR_Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <SelectiveScrollingGrid>
                                <SelectiveScrollingGrid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </SelectiveScrollingGrid.ColumnDefinitions>
                                <SelectiveScrollingGrid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                </SelectiveScrollingGrid.RowDefinitions>
                                <DataGridCellsPresenter Grid.Column="1" ItemsPanel="{TemplateBinding ItemsPanel}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                <DataGridDetailsPresenter Grid.Column="1" Grid.Row="1" SelectiveScrollingGrid.SelectiveScrollingOrientation="{Binding AreRowDetailsFrozen, ConverterParameter={x:Static SelectiveScrollingOrientation.Vertical}, Converter={x:Static DataGrid.RowDetailsScrollingConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" Visibility="{TemplateBinding DetailsVisibility}"/>
                                <DataGridRowHeader Grid.RowSpan="2" SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical" Visibility="{Binding HeadersVisibility, ConverterParameter={x:Static DataGridHeadersVisibility.Row}, Converter={x:Static DataGrid.HeadersVisibilityConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"/>
                            </SelectiveScrollingGrid>
                        </Border>
                    </ContentControl>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </DataGrid.RowStyle>
</DataGrid>

你需要做的另一件事就是修改你的RowSelect()方法,使其接受一个参数,这个参数应该是你在这里使用的任何类型(我只是假设它是'Movie'类型)。
public void RowSelect(Movie movie)
{
   // do something with 'movie'
}

0

我的例子中有一个名为“service_id”的列。但是您也可以使用int32列偏移量。 DataRowView类型中甚至有一个ItemArray来运行和下降。请参见System.Data命名空间。 您的Datagrid项源/上下文将影响您在Datagrid中看到的“对象”。但是,如果您在调试中检查类型,那么您可以将它们强制转换并使用它们。

private void DataGridServiceRegistry_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    DataGrid DGSR = (DataGrid) sender;
    var SR = (DataRowView) DGSR.CurrentItem;
    var service_id = SR.Row["SERVICE_ID"];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接