【问题标题】:Getting row information after a doubleclick双击后获取行信息
【发布时间】:2012-03-18 05:28:56
【问题描述】:

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

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 视图模型):

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

非常感谢!

【问题讨论】:

标签: c# .net xaml mvvm caliburn.micro


【解决方案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 中的事实。
【解决方案2】:

您可以在 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,此解决方案还将处理列标题上的双击,这几乎没有必要。为防止这种情况,请考虑以下 David Kiff 的解决方案。
【解决方案3】:

您可以通过修改 DataGrid 公开的 DataGridRows 的控件模板来完成此操作。下面的示例使用 WPF 和 Aero 主题。

我所做的唯一一件事是删除您之前的 cal:Message.Attach 调用并将其移动到围绕“默认”控件模板中的边框 (x:Name=DGR_Border) 的新“占位符”ContentControl。 (我使用 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】:

    我的示例中有一个名为“service_id”的列。但是您也可以使用 int32 列偏移量。 DataRowView TYPE 中甚至还有一个ItemArray 来运行和关闭。请参阅System.Data 命名空间。 您的 Datagrid itemssource / context 将影响您在 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"];
    }
    

    【讨论】:

      【解决方案5】:

      您也可以这样做:

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

      【讨论】:

      • 这是比接受的答案更好的解决方案,因为它只捕获对行而不是标题的双击。
      • 这是我一直在寻找的答案,应该标记为答案。
      • 这是正确答案!我还要补充一点,如果你想调用 DataGrid 的 DataContext 上的方法,还可以添加以下 Setter:&lt;Setter Property="cal:Action.TargetWithoutContext" Value="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}" /&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 2014-08-21
      • 1970-01-01
      相关资源
      最近更新 更多