专注于最后一行,在Datagrid中加载itemsource后开始编辑

时间:2018-08-17 11:45:52

标签: c# wpf datagrid

我想像datagrid一样出色,其中DataGrid最初添加了3个空行。由用户到达最后一行时,在编辑单元格时,可以通过// SHOW DIV 3 SECONDS BEFORE VIDEO STARTS (function($) { $(document).on("click", "a.short-button", function videoStart(callback) { $('.short_preloader').fadeIn().delay(3000).fadeOut(); callback(); }); // DO THIS FUNCTION AFTER THE DELAY videoStart(function() { var vidframe = $('.embed-container.short-embed iframe'), status = $('.status'); vidframe(function() { var player = $f(this); player.api('play'); }); }); })(jQuery);方法添加更多行,您可以看到AddMoreValues()事件。在此代码下工作正常。但是,现在我想集中讨论BeginningEdit事件之前的最后一行。该怎么做?

关于构造函数:

AddMoreValues()

当用户到达最后一行并开始编辑时,我添加了更多行。对于更多行,我编写了void AddValues() { Datagrid.ItemsSource = null; List<Item> list = new List<Item>(); list.Clear(); for(int i = 0; i<3; i++) { string number = i.ToString(); Item item = new Item() { Code = "", Quantity = "" }; list.Add(item); } Datagrid.ItemsSource = list; } 事件,并应用了以下条件:当要编辑的行与行索引相等时,将添加更多行,如您所见BeginningEdit方法。

AddMoreValues()

AddMoreValues():

private void Datagrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
            header = (string)Datagrid.SelectedCells[0].Column.Header;

            int counts = Datagrid.Items.Count;
            DataGridCellInfo cell = Datagrid.SelectedCells[0];
            var generator = Datagrid.ItemContainerGenerator;
            int columnIndex = cell.Column.DisplayIndex;
            int rowIndex = generator.IndexFromContainer(generator.ContainerFromItem(cell.Item)) + 1;

            //MessageBox.Show(rowIndex.ToString());

            //MessageBox.Show(counts + "," + rowIndex);
            if (rowIndex == counts)
            {
                AddMoreValues();

            }

Datagrid:

 void AddMoreValues()
        {

            List<Item> list = null;

            var GridList = (IList<Item>)Datagrid.ItemsSource;
            list = new List<Item>(GridList);

            Datagrid.ItemsSource = null;


            for (int i = 0; i < 3; i++)
            {
                string number = i.ToString();
                Item item = new Item()
                {
                    Code = "",
                    Quantity = ""
                };
                list.Add(item);
            }
            Datagrid.ItemsSource = list;
        }

为了重点,我已经在<DataGridTextColumn Header="Code" Width="1*" Binding="{Binding Code, Mode=TwoWay}"/> <DataGridTextColumn Header="Name" Width="1*" Binding="{Binding Name, Mode=TwoWay}"/> <DataGridTextColumn Header="Description" Width="1*" Binding="{Binding Description, Mode=TwoWay}"/> <DataGridTextColumn Header="Quantity" Width="1*" Binding="{Binding Quantity, Mode=TwoWay}"> 方法之后编写了这段代码,但是没有用

AddMoreValues()

1 个答案:

答案 0 :(得分:1)

只要您要编辑特定的单元格,就可以尝试执行以下操作:

Datagrid.Dispatcher.BeginInvoke(new Action(() =>
            {
    DataGrid grid = Datagrid;
    int rowIndex = 0;
    int columnIndex = 0;
    DataGridRow rowContainer = grid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
    if (rowContainer != null)
    {
        DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(rowContainer);
        if (presenter != null)
        {
            DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex) as DataGridCell;
            if (cell != null)
            {
                DataGridCellInfo dataGridCellInfo = new DataGridCellInfo(cell);
                if (!grid.SelectedCells.Contains(dataGridCellInfo))
                {
                    grid.SelectedCells.Add(dataGridCellInfo);
                }
                grid.CurrentCell = dataGridCellInfo;
                grid.BeginEdit();
            }
        }
    }
}), DispatcherPriority.Background);