WPF DataGridTextColumn 直接编辑

WPF DataGridTextColumn direct edit

我有非常简单的 DataGridTextColumn 应该在双击事件上修改。 问题是应该添加什么以避免异常 System.InvalidOperationException: ''EditItem' is not allowed for this view.'

<DataGrid x:Name="DG" ItemsSource="{Binding}" GridLinesVisibility="None" Grid.Column="3" Grid.Row="2">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding VariantSet, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="60" />
    </DataGrid.Columns>
</DataGrid>

简单class:

Public Class CName
    Public Property Name As String = "not editable name"
End Class

on load 只是添加到数据网格

Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    Me.DG.Items.Add(New CName)
End Sub

如下通过模板声明时,没有区别,同样的错误

        <DataGridTemplateColumn Header="Name" IsReadOnly="False">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Name}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

即使将 Implements ComponentModel.INotifyPropertyChanged 添加到 CName,也没有区别

你没有向我们展示你做错了什么。

这是一个可以使用的 window 数据网格。

代码是 c#,但如果您特别需要 vb,您可以通过转换器 运行 将其 vb。我认为现在初学者选择 vb 是个坏主意。几乎没有人使用 vb.

发布样本

我主要window:

<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
    <DataGrid AutoGenerateColumns="False"
              ItemsSource="{Binding People}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name"/>
            <DataGridTextColumn Binding="{Binding LastName}" Header="SurName"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

那个视图模型:

public class MainWindowViewModel : BaseViewModel
{
    private ObservableCollection<Person> people = new ObservableCollection<Person>();

    public ObservableCollection<Person> People
    {
        get { return people; }
        set { people = value; }
    }

    public MainWindowViewModel()
    {
        People.Add(new Person { FirstName = "Chesney", LastName = "Brown" });
        People.Add(new Person { FirstName = "Gary", LastName = "Windass" });
        People.Add(new Person { FirstName = "Liz", LastName = "McDonald" });
        People.Add(new Person { FirstName = "Carla", LastName = "Connor" });
    }
}

人只有名字和姓氏这两个属性:

public class Person : BaseViewModel
{
    private string firstName;

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; RaisePropertyChanged(); }
    }
    private string lastName;

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; RaisePropertyChanged(); }
    }

继承自刚刚实现 inotifypropertychanged 的​​ BaseViewmodel。

public  class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

这里我正在编辑一行