0

我有一个DataGrid和两个向上和向下按钮,以便用户可以在单击按钮时滚动浏览每个数据网格项目;向上或向下。我该怎么做呢?我已经尝试过SelectedIndex,但似乎不适用于DataGrid.

示例代码:

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

1 回答 1

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];
    }
}
于 2013-08-07T12:27:48.603 回答