1
votes

I have a data grid in my silverlight 4 application. I have also made a custom textbox that only takes numeric values and does a couple of other specific things. I also added a decimal dependency property on my custom control that I bind to, that pretty much returns a formatted version of Text that the control inherits from TextBox.

I then bind the datagrid's itemssource to an observablecolletion. I also added my custom control as one of the columns in the datagrid using DataGridTemplateColumn.

Everything works fine except the datagrid's virtualization. If I add an item to the dataGrid, edit the value in my custom Control, delete the row and then add a brand new row (a complete new instance) the value in my custom control is the value of the deleted row. And to clear up, it is not a binding mistake or anything like that. The problem is that the constructor of my custom textbox is not being called and also the binding is not updated until i change the actual value in my custom control. The datagrid's virtualization is re-using previously created controls.

I do not want to turn off virtualization. Is there a way to specify how my custom control should be recycled (virtualized)? Or perhaps an event that is fire when Controls are virtualized or recycled?

Any help will be much appreciated.

1

1 Answers

0
votes

Is there any chance you can upgrade to Silverlight 5? In SL 5 you have the DataContextChanged event which I think should be a good way of detecting when the control is reused to draw another row.

If SL 5 is not an option you may be able to catch it in the Loaded event. I am not entirely sure, but I think Loaded should be fired when the control is reused. Then whenever Loaded is fired, save the current DataContext to a temp variable. When Loaded is fired next time you can compare the old DataContext to the current one and if it has changed, re-run your logic as needed.