如何在按钮上单击wpf滚动每个Datagrid项目

时间:2013-08-07 11:40:10

标签: c# .net wpf

我有一个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;
    }

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