WPF DataGrid-如何在新添加的行上自动设置EditMode?

到目前为止,我有一个程序遵循MVVM原理/规则,后面有0个代码,还有一个DataGrid,用户可以在其中添加,编辑或删除代表学生的行。 用户可以通过单击“ +”按钮将行添加到列表中,但是要编辑该行,用户必须首先单击他刚刚添加的行,但这并不是那么用户友好。

我一直在尝试在EditMode中设置新添加的行,但是我的所有尝试都失败或起作用,但对程序的其余部分产生了一些令人不安的副作用。我在网上查询了一下,发现解决方案看起来像是过分杀伤,还是有不好的副作用。

我用更少的代码创建了一个类似的程序,以使其更易于显示程序和DataGrid的结构:

型号

public class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    public string PhoneNumber { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
}

ViewModel

public class StudentsViewModel : INotifyPropertyChanged
{
    public StudentsViewModel()
    {
        Students = new ObservableCollection<Student>();
    }

    private ObservableCollection<Student> students;
    public ObservableCollection<Student> Students
    {
        get { return students; }
        set
        {
            students = value;
            NotifyPropertyChanged(nameof(Students));
        }
    }

    private Student selectedStudent;
    public Student SelectedStudent
    {
        get { return selectedStudent; }
        set
        {
            selectedStudent = value;
            NotifyPropertyChanged(nameof(SelectedStudent));
        }
    }

    private ICommand addRow;
    public ICommand AddRow
    {
        get
        {
            if(addRow == null)
            {
                addRow = new RelayCommand(
                    parameter => AddStudent(new Student()),parameter => true
                );
            }
            return addRow;
        }
    }

    private ICommand removeCmd;
    public ICommand RemoveCmd
    {
        get
        {
            removeCmd = new RelayCommand(
                parameter => RemoveStudent(parameter as Student),parameter => parameter != null
            );
            return removeCmd;
        }
    }

    private void AddStudent(Student studentToAdd)
    {
        Students.Add(studentToAdd);
    }

    private void RemoveStudent(Student studentToRemove)
    {
        if (Students.Contains(studentToRemove))
        {
            Students.Remove(studentToRemove);
        }
    }

    #region INotify

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

查看

<Window x:Class="DataGridExample.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:DataGridExample"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="600"
        Width="1000">
    <Window.Resources>
        <local:StudentsViewModel x:Key="StudentsVm"/>
    </Window.Resources>
    <Grid DataContext="{Binding Source={StaticResource StudentsVm}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <DockPanel LastChildFill="False"
                   Background="#Ff2c58EC">
            <Button Command="{Binding AddRow}"
                    Height="25"
                    Margin="5">
                <Button.Template>
                    <ControlTemplate>
                        <Image Source="/Images/AddItem.png"/>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button Command="{Binding RemoveCmd}"
                    commandparameter="{Binding ElementName=StudentDataGrid,Path=SelectedItem}"
                    Height="25"
                    Margin="5">
                <Button.Template>
                    <ControlTemplate>
                        <Image Source="/Images/DeleteItem.png"/>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </DockPanel>
        <DataGrid ItemsSource="{Binding Students}"
                  SelectedItem="{Binding Source={StaticResource StudentsVm},Path=SelectedStudent,Mode=TwoWay}"
                  x:Name="StudentDataGrid"
                  ColumnWidth="*"
                  CanUserAddRows="False"
                  CanUserResizeRows="False"
                  CanUserResizeColumns="False"
                  CanUserSortColumns="False"
                  CanUserDeleteRows="False"
                  AutoGenerateColumns="False"
                  Grid.Row="1">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding FirstName,Mode=TwoWay,UpdateSourceTrigger=LostFocus}"
                                    Header="First Name">
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding LastName,UpdateSourceTrigger=LostFocus}"
                                    Header="Last Name">
                </DataGridTextColumn>
                <DataGridTemplateColumn Header="Date of Birth">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding DateOfBirth,StringFormat={}{0:dd.MM.yyyy},UpdateSourceTrigger=LostFocus}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding DateOfBirth,UpdateSourceTrigger=LostFocus}"
                                        DisplayDate="{Binding DateOfBirth,Mode=OneWay,UpdateSourceTrigger=LostFocus}">
                            </DatePicker>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding PhoneNumber,UpdateSourceTrigger=LostFocus}"
                                    Header="Phone Number">
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Address,UpdateSourceTrigger=LostFocus}"
                                    Header="Address">
                </DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding Email,UpdateSourceTrigger=LostFocus}"
                                    Header="Email">
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

我希望该解决方案与MVVM兼容,但老实说,只要它不会引起其他问题并且不需要大量的框架或充满代码的多个文件,我会对此感到满意。

我希望结果看起来像this example,但是只要{+3按钮单击后没有鼠标移动,this is also acceptable就可以了。

lvshanglin 回答:WPF DataGrid-如何在新添加的行上自动设置EditMode?

我找到了一种实现所需目标的方法,但是它似乎并不十分健壮,因此请谨慎使用。首先,您应该连接到LoadingRow的{​​{1}}事件。

XAML

DataGrid

隐藏代码

<DataGrid ...
          LoadingRow="StudentDataGrid_LoadingRow"
          ...>

通过制作方法private async void StudentDataGrid_LoadingRow(object sender,DataGridRowEventArgs e) { // Force asynchrony to run callback after UI has finished loading. await Task.Yield(); // Mark the new row as selected StudentDataGrid.SelectedItem = e.Row.Item; StudentDataGrid.CurrentCell = new DataGridCellInfo(e.Row.Item,StudentDataGrid.Columns[0]); // Enter edit mode StudentDataGrid.BeginEdit(); } 并强制其通过async调用来异步执行,可以让UI在要求通过{{1 }}。

我将其更多地作为实验来编写,我不知道我是否会推荐这样做,但是希望有人找到更好的解决方案时它会有所帮助。

本文链接:https://www.f2er.com/3143975.html

大家都在问