我有一个DataGrid
和两个向上和向下按钮,以便用户可以在按钮点击时滚动浏览每个数据网格项;向上或向下。我该怎么做呢?我尝试了SelectedIndex
但似乎不适用于DataGrid
。
ExampleCode:
private void btnDownCheckedMedication_Click(object sender, RoutedEventArgs e)
{
if (MedicationCheckedInDatagrid.SelectedIndex > 0)
{
MedicationCheckedInDatagrid.SelectedIndex = MedicationCheckedInDatagrid.SelectedIndex - 1;
}
}
private void btnUpCheckedMedication_Click(object sender, RoutedEventArgs e)
{
MedicationCheckedInDatagrid.SelectedIndex = MedicationCheckedInDatagrid.SelectedIndex + 1;
}
答案 0 :(得分:1)
你几乎拥有它......改为使用SelectedItem
:
private void btnDownCheckedMedication_Click( object sender, RoutedEventArgs e )
{
if(dataGrid.SelectedIndex > 0 ) {
dataGrid.SelectedItem = dataGrid.Items[dataGrid.SelectedIndex - 1];
}
}
private void btnUpCheckedMedication_Click( object sender, RoutedEventArgs e )
{
if(dataGrid.SelectedIndex < dataGrid.Items.Count - 1) {
dataGrid.SelectedItem = dataGrid.Items[dataGrid.SelectedIndex + 1];
}
}