locked
Ria MVVM Datagrid does not refresh on rejectChanges RRS feed

  • Question

  • I have a datagrid using a template column to display data from a ViewModel.  The cell template is a textblock, the cell editing template is a textbox.

    Each row in the grid is bound to a viewModel in a ICollectionView.

    Everything works correctly, except.

     

    A cell value exists in the textblock value = 10

    The cell is edited and the textbox value = 11, textBlock value = 11

    I call rejectChanges on the context

    The textBlock value = 11

    The textBox value = 10

     

    The textBlock does not revert but the TextBox does revert.

     

    To make it even more strange rows furthur down the grid work correctly.  Both the textBlock and textBox revert to 10 when RejectChanges is called.

     

    Any ideas?

    Tuesday, February 28, 2012 3:23 PM

All replies

  • Hi Shipswake,

     Both the textBlock and textBox revert to 10 when RejectChanges is called.

    If possible, could you please simplify your project and upload it onto SkyDrive then we can test it on our side.

    Thank you for your support!

    Thursday, March 1, 2012 4:36 AM
  • Hi Catherine,

    I am not really able to do that.  It is a very extensive product.

    I can post some code if it helps.

    Thursday, March 1, 2012 11:55 AM
  • Datasource is a ViewModel INotifyPropertyChanged, INotifyDataErrorInfo

    Grid ItemsSource is a ICollectionView of ViewModels

    public class ViewModel : ViewModelBase

    {

    public ICollectionView ListView = new PagedCollectionView(rowViewModels);

    }

    Each row is a view model

    public class RowViewModel : ViewModelBase

    {

    public double? PropertValue

    }

    The grid bindings on the view

    private void CreatePropertyColumn()

    {

    var dataGridTemplateColumn = new DataGridTemplateColumn

    {

    Header = ApplicationStrings.PropertyColumnName,

    SortMemberPath = "PropertyValue"

    };

    var binding = new Binding { Path = new PropertyPath("PropertyValue") };

    dataGridTemplateColumn.ClipboardContentBinding = binding;

    string headerTemplate = HeaderTemplate(string.Empty);

    dataGridTemplateColumn.HeaderStyle = (Style)XamlReader.Load(headerTemplate);

    string cellTemplate = PropertyCellTemplate();

    dataGridTemplateColumn.CellTemplate = (DataTemplate)XamlReader.Load(cellTemplate);

    string cellEditingTemplate = PropertyCellEditingTemplate();

    dataGridTemplateColumn.CellEditingTemplate = (DataTemplate)XamlReader.Load(cellEditingTemplate);

    grdPlanningAccount.Columns.Add(dataGridTemplateColumn);

    }

    private static string PropertyCellTemplate()

    {

    var stringBuilder = new StringBuilder();

    stringBuilder.Append("<DataTemplate ");

    stringBuilder.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");

    stringBuilder.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");

    stringBuilder.Append("xmlns:helpers='clr-namespace:Helpers;assembly='> ");

    stringBuilder.Append("<Grid>");

    stringBuilder.Append("<Grid.Resources>");

    stringBuilder.Append("<helpers:GridCellEditBackgroundStyleConverter x:Name='GridCellEditBackgroundStyleConverter' />");

    stringBuilder.Append("<helpers:GridCellEditBorderStyleConverter x:Name='GridCellEditBorderStyleConverter' />");

    stringBuilder.Append("<helpers:NullValueConverter x:Name='NullValueConverter' />");

    stringBuilder.Append("</Grid.Resources>");

    stringBuilder.Append("<Border Padding='0'>");

    stringBuilder.Append("<Border ToolTipService.ToolTip='{Binding PropertyToolTip}' BorderBrush='{Binding IsEnabled, Mode=OneWay, Converter={StaticResource GridCellEditBorderStyleConverter} }' Background='{Binding IsEnabled, Converter={StaticResource GridCellEditBackgroundStyleConverter} }' Style='{StaticResource GridCellEditBorderStyle}' >");

    stringBuilder.Append("<TextBlock Text='{Binding PropertyValue, Mode=TwoWay}' Style='{StaticResource GridTextBlockStyle}' />");

    stringBuilder.Append("</Border>");

    stringBuilder.Append("</Border>");

    stringBuilder.Append("</Grid>");

    stringBuilder.Append("</DataTemplate>");

    return stringBuilder.ToString();

    }

    private static string PropertyCellEditingTemplate()

    {

    var stringBuilder = new StringBuilder();

    stringBuilder.Append("<DataTemplate ");

    stringBuilder.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");

    stringBuilder.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");

    stringBuilder.Append("xmlns:helpers='clr-namespace:Helpers;assembly='> ");

    stringBuilder.Append("<Grid>");

    stringBuilder.Append("<Grid.Resources>");

    stringBuilder.Append("<helpers:GridCellEditBorderStyleConverter x:Name='GridCellEditBorderStyleConverter' />");

    stringBuilder.Append("<helpers:NullValueConverter x:Name='NullValueConverter' />");

    stringBuilder.Append("</Grid.Resources>");

    stringBuilder.Append("<TextBox Text='{Binding PropertyValue, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, Converter={StaticResource NullValueConverter} }' IsEnabled='{Binding IsEnabled, Mode=OneWay}' HorizontalAlignment='Stretch' />");

    stringBuilder.Append("</Grid>");

    stringBuilder.Append("</DataTemplate>");

    return stringBuilder.ToString();

    }

    Thursday, March 1, 2012 3:30 PM