双击后获取行信息

本文关键字:信息 获取 双击 | 更新日期: 2023-09-27 18:26:13

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

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?
     }

非常感谢!

双击后获取行信息

您也可以这样做:

<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?
}

您只需在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
}

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

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

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

我所做的唯一一件事是删除您以前的cal:Message.Attach调用,并将其移动到一个新的"占位符"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'
}

在我的示例中,有一列的名称为"service_id"。但是您也可以使用int32列偏移量。DataRowView TYPE中甚至有一个ItemArray要运行和关闭。请参见System.Data命名空间。您的数据网格项源/上下文将影响您在数据网格中看到的"对象"。但是,如果签入调试类型,则可以强制转换并使用它们。

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