【问题标题】:Select and edit the first DataGrid cell in a newly added row选择并编辑新添加行中的第一个 DataGrid 单元格
【发布时间】:2017-09-13 15:11:49
【问题描述】:

我有一个带有“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 的那一刻,视觉元素还不存在。有没有办法强制单元格加载以便能够聚焦和编辑它?或者当单元格准备好时我可以订阅一个事件吗?

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案1】:

    或者当单元准备好时我可以订阅一个事件吗?

    使用调度器:

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

    【讨论】:

    • 比所有其他解决方案好 100+
    • SelectionUnit="Cell", SelectionMode="Single",我删除了上面的 AddItem 代码 - 关注新的(尚未提交的)DataGrid 行
    【解决方案2】:
    DataGridCell cell = GetCell(1, 0); //row, column
    if (cell != null)
    {
        cell.Focus();
        yourDataGrid.BeginEdit();
    }
    

    GetCell的代码可以在下面找到

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

    【讨论】:

      猜你喜欢
      • 2014-03-07
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-06
      • 2011-07-25
      • 2013-12-14
      相关资源
      最近更新 更多