0

I have a pretty simple CRUD app which displays data from a database in a datagrid. This works fine. I'd like to have a comment column in the datagrid, where each row has a textbox that the user can insert a comment into. The problem is, the property in the class displaying data doesn't get updated with the comment. I can use a simple DataGridTextColumn with IsReadonly="False" for that column. This actually gets the comments, but it look horrible, and the user has to double-click the cell to enter it. With a textbox, validation looks smoother and the user only has to click once to enter it, but here the binding doesn't work. I've tried different variantions of DataGridTemplateColumn but I can't seem to get it right or find a working example. What am I missing?

<DataGridTemplateColumn Header="Comment" Width="120" IsReadOnly="False">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=UserComment}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                        <DataGridTemplateColumn.CellEditingTemplate>
                            <DataTemplate>
                                <TextBox IsEnabled="true" Text="{Binding Path=UserComment}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellEditingTemplate>
                    </DataGridTemplateColumn>
3
  • Could you please show us the code for the TextBox and also the viewmodel code for the comment property? Commented Jun 28, 2019 at 12:55
  • Try setting UpdateSourceTrigger property of the binding to 'PropertyChanged'. Otherwise it will be updated only upon lost focus. Commented Jun 28, 2019 at 13:07
  • I have implemented INotifypropertychanged in my ViewModel, and am using UpdateSourceTrigger=PropertyChanged, Mode=TwoWay in my binding. I just accidentally deleted them when I posted the question and renamed the Comment property to English. Sorry about that. I have a save button in my View so all data shown is saved to a database. When I click that button I get all the data from the View, except the comments. Those are not updated. Commented Jun 28, 2019 at 13:13

1 Answer 1

0

In your ViewModel, don't forget to notify property changed after updating the value. Else the view does not know it has to sync.

An elegant way to implement INotifyPropertyChanged

A basic way would be:

private string _userComment;

public string UserComment
{
   get { return _userComment; }
   set 
   {
      _userComment = value;
      PropertyChanged("UserComment");
   }
}

private void PropertyChanged(string prop)
{
   if( PropertyChanged != null )
   {
      PropertyChanged(this, new PropertyChangedEventArgs(prop);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.