1、DataGrid命名为planDataGrid
<DataGrid ItemsSource="{Binding PlanList}" SelectedItem="{Binding SelectedItem}" x:Name="planDataGrid" AutoGenerateColumns="False" CanUserAddRows="False"
? ? ? ? ? GridLinesVisibility="All" FontSize="12" ColumnHeaderHeight="42" RowHeaderWidth="0" EnableRowVirtualization="False" VirtualizingPanel.IsVirtualizing="False"></DataGrid>
2、??SetSelectedItemFirstRow(page.planDataGrid);调用方法,获取到前端界面的DataGrid,传给后端使用。
void SetSelectedItemFirstRow(object dataGrid)
{
//若目标datagrid为空,抛出异常
if (dataGrid == null)
{
throw new ArgumentNullException("目标无" + dataGrid + "无法转换为DataGrid");
}
//获取目标DataGrid,为空则抛出异常
System.Windows.Controls.DataGrid dg = dataGrid as System.Windows.Controls.DataGrid;
if (dg == null)
{
throw new ArgumentNullException("目标无" + dataGrid + "无法转换为DataGrid");
}
//数据源为空则返回
if (dg.Items == null || dg.Items.Count < 1)
{
return;
}
dg.SelectedItem = dg.Items[0];
dg.CurrentColumn = dg.Columns[0];
dg.ScrollIntoView(dg.SelectedItem, dg.CurrentColumn);
//获取焦点,滚动为目标行
dg.Focus();
dg.SelectedItem = selectedItem;//设置选中行
dg.CurrentColumn = dg.Columns[0];
dg.ScrollIntoView(dg.SelectedItem, dg.CurrentColumn);
var item = dg.SelectedItem as PlanDataModel;
ObservableCollection<CoilListModel> clm = new ObservableCollection<CoilListModel>();
DBHandle.GetCoilsByJobId(item, ref clm);
CoilList = clm;
page.coilDataGrid.ItemsSource = CoilList;
}