双击后获取行信息

时间:2012-02-28 17:48:21

标签: c# .net 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>

C#(MVVM ViewModel):

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

非常感谢!

5 个答案:

答案 0 :(得分:20)

您也可以这样做:

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

答案 1 :(得分:6)

使用Caliburn非常简单,只需在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?
}

<强> //修改 抱歉,只有当操作在datatemplate本身时,上述解决方案才有效...另一个解决方案是使用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
}

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

您可以通过修改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()方法来接受你在这里使用的任何类型的参数(我只是假设它是'电影'类型)。

public void RowSelect(Movie movie)
{
   // do something with 'movie'
}

答案 4 :(得分:0)

我的示例中有一个名为“service_id”的列。但是你也可以使用int32列偏移量。 DataRowView TYPE中甚至有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"];
}