DataGrid row button click
Folks, I think I've outsmarted myself. I have a DataGrid bound with a template column containing a Button and it's all looking quite acceptable. Now I'm a bit flummoxed, as when one of the buttons is clicked I have to find out which object in the data source is behind the row containing the button. Unlike the WinForms grid, I have no RowIndex or similar property to help me, all I have are two properties on the Click event: sender and e.OriginalSource, which seem to be the same. I'll probably have to walk backwards up the control tree somehow to find the row and the item it's bound to, but I don't know how yet. Has anyone done this and got clues? I'll keep web searching in the meantime. Greg ___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
RE: DataGrid row button click
If you are using MVVM, then you can bind your button to a command on the object being bound to. From: ozsilverlight-boun...@ozsilverlight.com [mailto:ozsilverlight-boun...@ozsilverlight.com] On Behalf Of Greg Keogh Sent: Tuesday, 6 December 2011 8:04 PM To: 'ozSilverlight' Subject: DataGrid row button click Folks, I think I've outsmarted myself. I have a DataGrid bound with a template column containing a Button and it's all looking quite acceptable. Now I'm a bit flummoxed, as when one of the buttons is clicked I have to find out which object in the data source is behind the row containing the button. Unlike the WinForms grid, I have no RowIndex or similar property to help me, all I have are two properties on the Click event: sender and e.OriginalSource, which seem to be the same. I'll probably have to walk backwards up the control tree somehow to find the row and the item it's bound to, but I don't know how yet. Has anyone done this and got clues? I'll keep web searching in the meantime. Greg ___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
RE: DataGrid row button click (answered?!)
To find the DataGridCell that contains the clicked button I found that a simple iteration up through the Parent properties of the sender eventually reaches the cell. I don't go to the trouble of using the VisualTreeHelper as the Parent property was enough in this case (I was doing that in a WPF app). The DataContext property of the cell can luckily be cast to the object type in the bound collection. I get the creeps about how fragile this all is, having no strongly typed or nicely indexed values to work with. Are we sure this is the way we're intended to do these things? I have this feeling of unease that there is some technique that I'm missing, but I doubt it. If you are using MVVM, then you can bind your button to a command on the object being bound to. How do you pass to the command handler something to identify the button/row/object being acted upon? Greg ___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
RE: DataGrid row button click (answered?!)
The row being acted upon is what actually has the Command object, so you're already in the context of what you want to get information from ViewModel: Public ObservableCollectionFooModel Foos { get; set; } FooModel: public string Name { get; set; } public RelayCommand AddWidget { get; set; } View.xaml: Datagrid ItemsSource={Binding Foos} Datagrid.Columns DataGridTemplateColumn Header=Execute! DataTemplate Button Command={Binding AddWidget} / .. Something like that (pseudocode) From: ozsilverlight-boun...@ozsilverlight.com [mailto:ozsilverlight-boun...@ozsilverlight.com] On Behalf Of Greg Keogh Sent: Tuesday, 6 December 2011 9:07 PM To: 'ozSilverlight' Subject: RE: DataGrid row button click (answered?!) To find the DataGridCell that contains the clicked button I found that a simple iteration up through the Parent properties of the sender eventually reaches the cell. I don't go to the trouble of using the VisualTreeHelper as the Parent property was enough in this case (I was doing that in a WPF app). The DataContext property of the cell can luckily be cast to the object type in the bound collection. I get the creeps about how fragile this all is, having no strongly typed or nicely indexed values to work with. Are we sure this is the way we're intended to do these things? I have this feeling of unease that there is some technique that I'm missing, but I doubt it. If you are using MVVM, then you can bind your button to a command on the object being bound to. How do you pass to the command handler something to identify the button/row/object being acted upon? Greg ___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
Re: DataGrid row button click
You should be able to grab the data context from sender without iterating the visual tree at all. Data contexts flow down automatically. Cheers, Jordan. On 06/12/2011, at 11:03 PM, Greg Keogh g...@mira.net wrote: Folks, I think I’ve outsmarted myself. I have a DataGrid bound with a template column containing a Button and it’s all looking quite acceptable. Now I’m a bit flummoxed, as when one of the buttons is clicked I have to find out which object in the data source is behind the row containing the button. Unlike the WinForms grid, I have no RowIndex or similar property to help me, all I have are two properties on the Click event: sender and e.OriginalSource, which seem to be the same. I’ll probably have to walk backwards up the control tree somehow to find the row and the item it’s bound to, but I don’t know how yet. Has anyone done this and got clues? I’ll keep web searching in the meantime. Greg ___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight ___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
Re: DataGridCheckBoxColumn (fixed?!)
Create a user control and place it in the cell. Have a dependency property on it to publish the select color back to where you need... Cheers, Jordan. On 06/12/2011, at 7:07 PM, Greg Keogh g...@mira.net wrote: Now that’s solved, I have an even more ambitious thing to try. I want to turn a DataGrid cell into a colour picker. I have a template column containing a Grid with a TextBlock inside. The grid background is bound to the colour (via a converter) and I can see a cell value like # with a red background. I now have to: 1. Display the ARGB value as a known colour (if it corresponds to one). 2. Make the mouse turn to a hand when over the colour cells. 3. Open some sort of colour picker when the colour cells are clicked and get that value back to the cell and bound property (luckily we’re using the C1 kit and I think they have a colour picker). Fun eh?! I estimate 6 hours work. In WinForms it would take 15 minutes, but I shouldn’t get sentimental. Greg ___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight ___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
RE: DataGridCheckBoxColumn (fixed?!)
It’s all working now. It turns out I didn’t need to walk up anywhere for the DataContext, as the I managed to embed a C1 colour picker control into the template cell and bind it directly to the Colour property. The screen shot I’ve pasted below (if your email can see it) shows how I’ve bound a grid to a data source with bool and Colour properties and it all works with little code. I did have to use template cells as the CheckBoxColumn has stupid behaviour I previous whinged about. The Colour cell is a Grid with a TextBlock and the picker. I used a converter to show the grid background colour and friendly text. I had many subtle problems with column sizing, binding choices and the poor C1 documentation about their picker, so it took many hours to get this innocuous looking screen working. I will soon need to have a Combobox in a DataGrid cell and I’m not expecting that experience to be hugs and puppies either -- Greg image001.png___ ozsilverlight mailing list ozsilverlight@ozsilverlight.com http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight