【问题标题】:Get selected row item in DataGrid WPF doesn't work在 DataGrid WPF 中获取选定的行项不起作用
【发布时间】:2014-12-04 01:05:57
【问题描述】:

我正在尝试获取选定的行项目。 我已经读过它应该起作用:

<DataGrid ItemsSource="{Binding Path=Customers}"
              SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"/>

Customer customer = (Customer)myDataGrid.SelectedItem;
  1. 在第一个 xaml 中 - 我放了它,它没有错误或者我只是不知道如何使用它。如何在 c# 代码中获取选定的行?

  2. 在 C# 行代码中出错。视觉工作室不存在客户。

    我很乐意提供帮助。 :) 谢谢。

【问题讨论】:

  • 绑定SelectedItem 的目的是能够通过SelectedCustomer 属性访问它,而不是通过后面代码中的Datagrid。在SelectedCustomer 的设置器中放置一个断点并检查其是否正常工作。
  • 我只是不知道如何使用它...教你如何编写 WPF 不是我们的工作。您需要使用书籍或外部资源自学。我们的工作是帮助您解决您在项目中遇到的具体问题

标签: c# wpf visual-studio xaml datagrid


【解决方案1】:

很难知道你到底想要什么 :) 但这里有一个我刚刚提出的关于如何将信息拖出数据网格的示例。 没有在其中实施很多这些绑定,所以这纯粹是获取信息。

Xaml

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <DataGrid Grid.Row="0" Name="dg"/>
    <Button Grid.Row="1" Name="btn" Click="btn_Click" />

</Grid>

代码隐藏

    List<SomeInfo> list = new List<SomeInfo>();

    public MainWindow()
    {
        InitializeComponent();
        list.Add(new SomeInfo() { Name = "PC", Description = "Computer", ID = 1 });
        list.Add(new SomeInfo() { Name = "PS", Description = "Playstation", ID = 2 });
        list.Add(new SomeInfo() { Name = "XB", Description = "Xbox", ID = 3 });
        this.dg.ItemsSource = list;
    }

    public class SomeInfo
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public int ID { get; set; }
    }

    private void btn_Click(object sender, RoutedEventArgs e)
    {
        if (dg.SelectedIndex != -1)
        {
            DataGrid dataGrid = sender as DataGrid;
            DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(dg.SelectedIndex);
            DataGridCell RowColumn = dg.Columns[0].GetCellContent(row).Parent as DataGridCell;
            btn.Content = ((TextBlock)RowColumn.Content).Text;
        }
    }

btn_click 完成所有收集信息的工作,最后 2 个使我的数据网格用于测试。

希望对你有帮助:)

------------------编辑------------------------- -

从下面的评论中你只需要这个

private void btn_Click(object sender, RoutedEventArgs e)
    {
        if (dg.SelectedIndex != -1)
        {
            DataGrid dataGrid = sender as DataGrid;
            DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(dg.SelectedIndex);
            DataGridCell RowColumn = dg.Columns[0].GetCellContent(row).Parent as DataGridCell;
            btn.Content = ((TextBlock)RowColumn.Content).Text;
        }
    }

dg = your datagrid 
dg.Columns[0] = change 0 into what column you want
info from btn.Content = what you want the content to be

-------------编辑 2------------ 要获取所选行的索引,您只需要

            int index = dg.SelectedIndex;
            btn.Content = index;

或者如果您不想存储整数

            btn.Content = dg.SelectedIndex;

【讨论】:

  • 对不起,我不明白我的代码应该写什么。我的项目中有很多变量不存在。
  • private void btn_Click(object sender, RoutedEventArgs e) { if (dg.SelectedIndex != -1) { DataGrid dataGrid = sender as DataGrid; DataGridRow 行 = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(dg.SelectedIndex); DataGridCell RowColumn = dg.Columns[0].GetCellContent(row).Parent as DataGridCell; btn.Content = ((TextBlock)RowColumn.Content).Text; } } dg = 您的数据网格 dg.Columns[0] = 将 0 更改为您想要来自 btn.Content 的信息的列 = 您想要的内容是什么
  • 我会编辑这篇文章,这样你就可以看到我刚刚写的更好的例子
  • 我只需要获取行索引,就是这样。对于删除更新和编辑行..我是该数据网格和 Access 数据库之间的连接,此代码中的行索引在哪里?因为这里没有什么是 int 或其他东西
  • 这很容易,只需制作 datagrid.selectedindex。如果需要,您可以将值放入 int 或将其放入您喜欢的位置,它将为您提供所选行的索引,您也可以检查我的最新编辑
【解决方案2】:

网格中定义的很不完整。 理事会明确所有组成它的列与单独的绑定

在这种情况下,我使用了事件 doubleClick,但您必须为自己选择正确的事件

例如通过代码试试这个:

在 XAML 中添加:

<DataGrid x:Name="MyDataGrid" x:FieldModifier="public" MouseDoubleClick="MyDataGrid_MouseDoubleClick" ItemsSource="{Binding Path=Customers}" 
              SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"/>

在后面的 c# 代码中:

private void MyDataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender != null)
            {
                DataGrid grid = sender as DataGrid;
                if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1)
                {

                        var selectedRow = grid.SelectedItem as MyObject;

                        try
                        {
                            Mouse.OverrideCursor = Cursors.Wait;

                            //TODO YOUR OPERATION ON selectdRow
                        }
                        finally
                        {
                             Mouse.OverrideCursor = null;
                        }

                }
            }
        }

【讨论】:

    猜你喜欢
    • 2011-04-24
    • 2012-12-16
    • 2013-10-14
    • 2018-07-27
    • 2016-10-13
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    相关资源
    最近更新 更多