Silverlight 3 button click argument
I'm just beginning with Silverlight. I have a DataGrid whose ItemsSource is linked to an ObservableCollection. Each row of the DataGrid (AutoGenerateColumns= "False") has some values from an object and a button. How can I associate a "command argument" to the button so that I know what button of what row is being clicked?
Thanks开发者_如何学Python
You can use Tag attrribute for that. Assuming that the objects in the collection have "Id" property you can bind the "Tag" attribute of the button control to the Id of the object with:
<Button Tag="{Binding Id}" .../>
or you can store the entire object in Tag
<Button Tag="{Binding Path=.}" .../>
Then in click handler you have to use the sender parameter to get the sender control and extract the tag info:
Button btn = sender as Button;
var tagValue = btn.Tag; // object binded to "tag" attribute
I've found about the DataContext
property of the Button objects that, in this case, returns the object on which the row is based.
精彩评论