在DataGrid中获取所选行项目WPF不';不起作用

本文关键字:WPF 不起作用 项目 DataGrid 获取 | 更新日期: 2023-09-27 18:26:08

输入字符串以获取所选行项目。我已经准备好了,它应该工作:

<DataGrid ItemsSource="{Binding Path=Customers}"
              SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"/>
Customer customer = (Customer)myDataGrid.SelectedItem;
  1. 在我放的第一个xaml中,它没有错误,或者我只是不知道如何使用它。在c#代码中,我如何获得所选的行?

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

    我愿意帮忙谢谢

在DataGrid中获取所选行项目WPF不';不起作用

很难知道你到底想要什么:)但这里有一个我刚刚介绍的关于如何将信息从数据网格中拖出来的例子。没有在里面植入太多绑定,所以这纯粹是获取信息。

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

btnclick完成了所有收集信息的工作,最后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;

在中定义的网格非常不完整。理事会明确所有组成它的专栏与个人有约束力的

在这种情况下,我使用了事件双击,但你必须为选择正确的事件

例如,通过代码试试这个:

在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;
                        }
                }
            }
        }