简体   繁体   English

从Datagrid单元格或行获取文本

[英]Get Text from Datagrid cell or Row

Hello i am using the data grid to show an observable collection in WPF. 您好,我正在使用数据网格在WPF中显示可观察的集合。 Now how can i get the selected row text from the DataGrid so i can call a function. 现在如何从DataGrid中获取选定的行文本,以便调用函数。 Here is my XAML: 这是我的XAML:

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="0,77,0,0" VerticalAlignment="Top" Height="720" Width="664" ItemsSource="{Binding Items}" AutoGenerateColumns="False" Grid.ColumnSpan="2" SelectedCellsChanged="dataGrid_SelectedCellsChanged">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ProjectName" MinWidth="100" Binding="{Binding Path=ProjectName,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Header="Name" MinWidth="100" Binding="{Binding Path=Name,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
            <DataGridTextColumn Header="Path" MinWidth="460" Binding="{Binding Path=Path,Mode=TwoWay}" IsReadOnly="True"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

How can i get the text from the selected cell or row? 如何从选定的单元格或行中获取文本?

As WPF DataGrid is more flexible than a WinForms DataGridView , gettings values seems difficult. 由于WPF DataGridWinForms DataGridView更灵活,因此获取值似乎很困难。 So I made the below static extension methods/funcitons to get SelectedRow , SelectedCell & SelectedCellValue . 所以我做了下面的static 扩展方法/功能来获取SelectedRowSelectedCellSelectedCellValue Credits to some post I saw a while back in SO for GetVisualChild and some of the functions given below. GetVisualChild我在GetVisualChild和下面给出的一些功能中看到的帖子。

public static T GetVisualChild<T>(Visual parent) where T : Visual
{
     T child = default(T);
     int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
     for (int i = 0; i < numVisuals; i++)
     {
         Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
         child = v as T;
         if (child == null)
         {
            child = GetVisualChild<T>(v);
         }
         if (child != null)
         {
            break;
         }
    }
        return child;
}

To get SelectedRow : 要获取SelectedRow

public static DataGridRow GetSelectedRow(this DataGrid grid)
{
    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}

Gets the DataGridRow when you know the RowIndex : 当您知道RowIndex时,获取DataGridRow

public static DataGridRow GetRow(this DataGrid grid, int index)
{
     DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
     if (row == null)
     {
        grid.UpdateLayout();
        grid.ScrollIntoView(grid.Items[index]);
        row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
     }
     return row;
}

Gets the DataGridCell when you have a DataGridRow and a columnIndex : 当您具有DataGridRowcolumnIndex时,获取DataGridCell

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int columnIndex)
{
    if (row != null)
    {
        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

        if (presenter == null)
        {
            grid.ScrollIntoView(row, grid.Columns[columnIndex]);
            presenter = GetVisualChild<DataGridCellsPresenter>(row);
        }

        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
        return cell;
    }
    return null;
}

Gets the DataGridCell when you have a DataGridRow and a columnName : 当您具有DataGridRowcolumnName时,获取DataGridCell

public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, string columnName)
{
    int index = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;
    return GetCell(grid, row, index);
}

Gets the DataGridCell when you have a rowIndex and a columnIndex : 当您具有rowIndexcolumnIndex时获取DataGridCell

public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
     DataGridRow rowContainer = grid.GetRow(row);
     return grid.GetCell(rowContainer, column);
}

Gets the DataGridCellValue when you have a DataGridRow and a ColumnName : 当您具有DataGridRowColumnName时,获取DataGridCellValue

public static string GetCellValue(this DataGrid grid, DataGridRow row, string columnName)
{
    int index = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;
    DataGridCell dgc = GetCell(grid, row, index);
    string str = Convert.ToString(((TextBlock)dgc.Content).Text);
    return str;
}

Gets the DataGridCellValue when you have a rowIndex and a ColumnName : 当您具有rowIndexColumnName时获取DataGridCellValue

public static string GetCellValue(this DataGrid grid, int rowIndex, string columnName)
{
     DataGridRow row = grid.GetRow(rowIndex);
     int columnIndex = grid.Columns.Single(c => c.Header.ToString().ToUpper() == columnName.ToUpper()).DisplayIndex;

     DataGridCell dgc = GetCell(grid, row, columnIndex);
     string str = Convert.ToString((TextBlock)dgc.Content);
     return str;
}

You should use the default CollectionView for your data source. 您应该为数据源使用默认的CollectionView。 Assumed it is called Items, you can use: 假设它称为项,则可以使用:

var cv = (CollectionView)CollectionViewSource.GetDefaultView(Items);
var currentItem = (YourItemType)cv.CurrentItem;

If the Items collection you bind to is an IEnumerable<T> , you could cast the SelectedItem property of the DataGrid to T : 如果绑定到的Items集合是IEnumerable<T> ,则可以将DataGridSelectedItem属性转换为T

YourItemType obj = dataGrid.SelectedItem as YourItemType;
string name = obj.Name;

Or you could bind the SelectedItem property to a property of type T , just as you bind the ItemsSource property: 或者,您可以将SelectedItem属性绑定到T类型的属性,就像您绑定ItemsSource属性一样:

<DataGrid x:Name="dataGrid" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" ...>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM