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 ObservableCollection<FooModel> 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

Reply via email to