1

The problem is, when I edit programmatically column values in datagrid and scroll down, those values repeat in next cells of column which become visible. This seems like virtualization bug or "effect" since when I switch it off, problem disappears. I think the problem is in this place:

public void EditedCell(object oItem, string oColumnName, ref List<string> lErrors, object newValue = null)
{
    DataGridRow dgRow = dgDati.ItemContainerGenerator.ContainerFromItem(oItem) as DataGridRow;
    /* In other places, when I call EditedCell(DataGridRow, ...) it works fine.
       The problem shows up only when I call EditedCell(object oItem, ...) */
    EditedCell(dgRow, oColumnName, ref lErrors, newValue);
}

This is the screen of problem. Yellow cells are programmatically changed and other 0000 containing cells are shown due to this problem. When I read data from DataSource it doesnt have those 0000 values in DataRows:

enter image description here

Also to set value of cell, I set cell controls in cell and DataRow value to change value and display it properly:

if (oElement.GetType() == typeof(TextBox))
{
    (oElement as TextBox).Text = newValue.ToString();
}

if (oElement.GetType() == typeof(TextBlock))
{
    (oElement as TextBlock).Text = newValue.ToString();
}

Have anyone seen anything similar and knows how to deal with that?

Jānis Gruzis
  • 905
  • 1
  • 10
  • 25
  • 2
    Quoting @HighCore's clincher - "Don't manipulate WPF control's through procedural code" :) [MVVM](http://msdn.microsoft.com/en-us/magazine/dd419663.aspx) will save you all this headache – Omri Btian Oct 16 '13 at 08:39
  • 1
    As @Omribitan has quietly hinted, manipulating UI controls programmatically is *not* the WPF way. In general, we `Bind` to the UI controls and manipulate the data instead... you'll get *far* fewer problems doing it this way. – Sheridan Oct 16 '13 at 08:43
  • 1
    @Omribitan Thanks for spreading the word =). Now I know I'm not alone. – Federico Berasategui Oct 16 '13 at 11:20
  • @Omribitan yeah, i did something along MVVM way and it worked. Please post some sort of an answer so I can approve. – Jānis Gruzis Oct 17 '13 at 10:44

1 Answers1

1

Don't manipulate WPF control's through procedural code. This is not the WPF way. In WPF you should manipulate your data which is binded to the UI, instead of manipulating the UI directly. This design pattern is known as MVVM and will spare you all this headache.

Community
  • 1
  • 1
Omri Btian
  • 6,499
  • 4
  • 39
  • 65