您好,我正在使用数据网格在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 DataGrid
比 WinForms DataGridView
更灵活,gettings值似乎很困难。因此,我制作了以下static
扩展名方法/funcitons以获取SelectedRow
,SelectedCell
&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;
}
有DataGridRow
和columnIndex
时获取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;
}
有DataGridRow
和columnName
时获得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);
}
有rowIndex
和columnIndex
时获取DataGridCell
:
public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
DataGridRow rowContainer = grid.GetRow(row);
return grid.GetCell(rowContainer, column);
}
有DataGridRow
和ColumnName
时获得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;
}
有rowIndex
和ColumnName
时获取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>
,则可以将DataGrid
的SelectedItem
属性施加到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;