I need to take some action (e.g. make some cells readonly based on some other cells) after data biinding is completed. In WinForm DataGridView, I used to do it in DataBindingComplete event. However, I couldn't find such an event in WPF DataGrid. What else can I use?
It is strange. Somehow DataContextChanged event doesn't work for me. What I want to do is to disable some cells in a column based on the value of another column in the same row. The same code works if I put it in a cell click event, but it doesn't work in DataContextChanged event. I have the feeling that the DataGrid is not ready yet when this event is fired, that's why my update is not taking effect - just a guess. Any clue?
miliu
2010-10-01 21:30:09
I think you can rather easily do this with an IValueConverter and a style setting. I could write a sample for this, but it would take a few days.
Chris Holmes
2010-10-02 03:05:25
I'm not sure if styles will solve all my problems in this respect. In my app in WinForm (which I'm trying to convert to WPF), a lot of DataGridView is used. In DataBindingComplete event, we do a bit more than just setup up visual effect. But styles will definitely save me some trouble.
miliu
2010-10-02 03:22:06
@Chris: I'm very interested in your suggestion of using IValueConverter and Style. Could you please provide me some sample code? Thanks!
miliu
2010-10-15 21:26:06
@miliu It appears that IsReadOnly cannot be set on an individual Cell basis with styles and IValueConverters. Only at the Column level. So, unfortunately, I can't provide any code that helps with your specific case. Sorry miliu. I was hoping it would work.
Chris Holmes
2010-10-18 14:20:06
@Chris: Thanks anyway. I hope microsoft will add the cell-level readonly read/write property in the next release. In the meantime, I guess I have to use WPF toolkit datagrid so that I can change the source code directly.
miliu
2010-10-21 18:54:25
A:
This is what I figured out: DataContextChanged event is the right event to use. The only problem is that the datagrid is not quite ready to be consumed in my code inside this event. However, it works fine if I use Dispatcher.BeginInvoke like this:
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => DoSomethingWithGrid()));
Can anybody explain why this is necessary?
Actually, when dealing with WPF DataGrid, I had to use Dispatcher in quite a few cases in order to make it work. Why?
miliu
2010-10-25 14:30:04