Search code examples
wpfvb.netdatagridtemplatecolumn

WPF DataGridTextColumn direct edit


i have very simple DataGridTextColumn which should be modified on doubleclick event. question is what should be added to avoid exception 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>

simple class:

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

on load simply added to datagrid

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

when declared through template as following, there is no difference, same error

        <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>

even when Implements ComponentModel.INotifyPropertyChanged is added to the CName, no difference


Solution

  • You've not shown us enough to tell what you're doing wrong.

    Here's a working window with datagrid.

    The code is c# but you can run it through a converter to vb if you particularly want vb. I think it's a bad idea for a beginner to choose vb nowadays. Almost nobody publishes samples using vb.

    In my mainwindow:

    <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>
    

    That viewmodel:

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

    Person just has those two properties first and last name:

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

    That inherits from BaseViewmodel which just implements inotifypropertychanged.

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

    Here I am editing a row

    enter image description here