从Datagrid单元格或行获取文本



您好,我正在使用数据网格在WPF中显示可观察的集合。现在,如何从数据杂志中获取所选的行文本,以便我调用函数。这是我的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>

如何从选定的单元格或行获得文本?

as WPF DataGridWinForms DataGridView更灵活,gettings值似乎很困难。因此,我制作了以下static 扩展名方法/funcitons以获取SelectedRowSelectedCell&amp;SelectedCellValue。记载了一些帖子,我回到了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;
}

获得SelectedRow

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

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

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

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

rowIndexcolumnIndex时获取DataGridCell

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

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

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

如果您绑定到的 Items集合是IEnumerable<T>,则可以将DataGridSelectedItem属性施加到T

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

或者您可以将SelectedItem属性绑定到类型T的属性,就像您绑定ItemsSource属性时:

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

您应该为数据源使用默认的CollectionView。假设它称为项目,您可以使用:

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

最新更新