0

我正在将学生加载到我的数据网格中,然后想将他们分配给一个班级。我真的不在乎到底如何,但是我无法使任何方法起作用。

我尝试使用上下文菜单,但没有设法获取事件(以前从未使用过它)然后我在每一行中添加了一个按钮,但后来我不知道如何获取按钮所在的行是。我最后一次尝试是在组合框上获取 selectedItem 并使用 ToString() ,但这也不起作用。

看一下这个:

    private void dataGrid4_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dg = new DataGrid();
        dg = (sender as DataGrid);
        MessageBox.Show(dataGrid4.Items.GetItemAt(1).ToString());
        MessageBox.Show(dataGrid4.CurrentItem.ToString() + "\n" + dataGrid4.CurrentCell.Column.ToString() + "\n" + dataGrid4.CurrentCell.Item.ToString() + "\n" + dataGrid4.SelectedItem.ToString() + "\n" + dataGrid4.SelectedValue.ToString() + "\n" + dataGrid4.SelectedIndex.ToString());
        MessageBox.Show(dg.CurrentItem.ToString() + "\n" + dg.CurrentCell.Column.ToString() + "\n" + dg.CurrentCell.Item.ToString() + "\n" + dg.SelectedItem.ToString() + "\n" + dg.SelectedValue.ToString() + "\n" + dg.SelectedIndex.ToString());
    }

请帮帮我,我快吓坏了..):

4

1 回答 1

4

您可以像这样从网格中获取选定的项目:

<DataGrid Name="dgStudents" 
                  AutoGenerateColumns="False"
                  SelectionChanged="dgStudents_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding ID}" />
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" />
            </DataGrid.Columns>
        </DataGrid>

事件处理程序:

private void dgStudents_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems != null && e.AddedItems.Count != 0)
            {
                foreach (Student item in e.AddedItems)
                {
                    Console.WriteLine(item.ID);
                }
            }
        }

简单的学生班:

class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }

如果你想使用上下文菜单试试这个:

<DataGrid Name="dgStudents" 
                  AutoGenerateColumns="False">
            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="ClassA" Click="ClassA_Click" />
                    <MenuItem Header="ClassB" Click="ClassB_Click" />
                </ContextMenu>
            </DataGrid.ContextMenu>

            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding ID}" />
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <DataGridTextColumn Header="Surname" Binding="{Binding Surname}" />
                <DataGridTextColumn Header="Class" Binding="{Binding ClassName}" />
            </DataGrid.Columns>
        </DataGrid>

MenuItem在代码隐藏中添加两个事件处理程序:

private void ClassA_Click(object sender, RoutedEventArgs e)
        {
            if (dgStudents.SelectedItems != null && dgStudents.SelectedItems.Count != 0)
            {
                foreach (Student item in dgStudents.SelectedItems)
                {
                    item.ClassName = "ClassA";
                }
                dgStudents.Items.Refresh();
            }
        }

        private void ClassB_Click(object sender, RoutedEventArgs e)
        {
            if (dgStudents.SelectedItems != null && dgStudents.SelectedItems.Count != 0)
            {
                foreach (Student item in dgStudents.SelectedItems)
                {
                    item.ClassName = "ClassB";
                }
                dgStudents.Items.Refresh();
            }
        }

将学生班级更改为:

 class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
        public string ClassName { get; set; }
    }
于 2012-11-22T22:09:01.453 回答