您好我正在使用数据网格在WPF中显示可观察的集合。 现在我如何从DataGrid中获取所选的行文本,以便我可以调用一个函数。 这是我的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>
如何从所选单元格或行中获取文本?
答案 0 :(得分:3)
由于WinForms DataGridView
比static
更灵活,获取值似乎很难。因此,我制作了以下SelectedRow
扩展名方法/功能,以获得SelectedCell
,SelectedCellValue
&amp; 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;
}
以及下面给出的一些功能,我在SO中看到了一些帖子。
SelectedRow
获取public static DataGridRow GetSelectedRow(this DataGrid grid)
{
return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}
:
DataGridRow
当您知道RowIndex
:
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;
}
DataGridCell
获得DataGridRow
和columnIndex
后的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;
}
:
DataGridCell
获得DataGridRow
和columnName
后的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);
}
:
DataGridCell
获得rowIndex
和columnIndex
后的public static DataGridCell GetCell(this DataGrid grid, int row, int column)
{
DataGridRow rowContainer = grid.GetRow(row);
return grid.GetCell(rowContainer, column);
}
:
DataGridCellValue
获得DataGridRow
和ColumnName
后的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;
}
:
DataGridCellValue
获得rowIndex
和ColumnName
后的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;
}
:
$result = DB::table('users')
->join('role_user', 'users.id', '=', 'role_user.user_id')
->where('role_user.role_id', $roleId)
->join('roles', 'role_user.role_id', '=', 'roles.id')
->select('users.*','role_user.role_id','roles.display_name','roles.description')
->get();
答案 1 :(得分:0)
您应该使用默认的CollectionView作为数据源。假设它被称为项目,您可以使用:
var cv = (CollectionView)CollectionViewSource.GetDefaultView(Items);
var currentItem = (YourItemType)cv.CurrentItem;
答案 2 :(得分:0)
如果您绑定的Items
集合是IEnumerable<T>
,则可以将SelectedItem
的{{1}}属性转换为DataGrid
:
T
或者您可以将YourItemType obj = dataGrid.SelectedItem as YourItemType;
string name = obj.Name;
属性绑定到类型为SelectedItem
的属性,就像绑定T
属性一样:
ItemsSource