选择并编辑新添加的行中的第一个DataGrid单元格

时间:2017-09-13 15:11:49

标签: c# wpf datagrid

我的数据网格为" CanUserAddRows = False"我想在按钮点击上添加一个新行并开始编辑它。这是xaml:

<Window x:Class="TestDataGrid2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestDataGrid2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Button Content="Add Item" HorizontalAlignment="Left" Click="Button_Click"/>
        <DataGrid ItemsSource="{Binding Items}" Grid.Row="1" Name="MyGrid" AutoGenerateColumns="False" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Value" Binding="{Binding Value}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

这是背后的代码:

using System.Windows;
using System.Windows.Controls;

namespace TestDataGrid2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var mainViewModel = DataContext as MainViewModel;
            if (mainViewModel != null)
                mainViewModel.AddItem();

            var grid = MyGrid;
            var cell = new DataGridCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
            grid.CurrentCell = cell;
            grid.BeginEdit();
        }
    }
}

这是我的观点模型:

using System.Collections.ObjectModel;

namespace TestDataGrid2
{
    public class MainViewModel
    {
        public ObservableCollection<Item> Items { get; set; } = new ObservableCollection<Item>();

        public void AddItem()
        {
            Items.Add(new Item { Value = "New Item" });
        }
    }

    public class Item
    {
        public string Value { get; set; }
    }
}

出于某种原因,我无法在新添加的行中编辑单元格。我怀疑这可能是因为我现在称之为BeginEdit,视觉元素还没有存在。有没有办法强制单元格加载,以便能够对焦和编辑它?或者,当单元格准备就绪时,我可以订阅一个事件吗?

2 个答案:

答案 0 :(得分:3)

  

或者,当单元格准备就绪时,我可以订阅一个事件吗?

使用调度程序:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var mainViewModel = DataContext as MainViewModel;
    if (mainViewModel != null)
        mainViewModel.AddItem();

    var grid = MyGrid;
    var cell = new DataGridCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
    grid.CurrentCell = cell;
    Dispatcher.BeginInvoke(new Action(() =>
    {
        grid.BeginEdit();
    }), System.Windows.Threading.DispatcherPriority.Background);
}

答案 1 :(得分:0)

DataGridCell cell = GetCell(1, 0); //row, column
if (cell != null)
{
    cell.Focus();
    yourDataGrid.BeginEdit();
}

GetCell的代码可以在下面找到

https://stackoverflow.com/a/1760908/7676905