In my Silverlight application I have defined a datagrid with an template column containing a radio button as follows:
XAML:
<data:DataGrid x:Name="Grid1" Margin="8">
<data:DataGrid.Columns>
<data:DataGridTemplateColumn Header="RadioButtons">
<data:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<RadioButton x:Name="rdbIndataGrid" IsChecked="false" GroupName="myGroup" />
</DataTemplate>
</data:DataGridTemplateColumn.CellTemplate>
</data:DataGridTemplateColumn>
</data:DataGrid.Columns>
</data:DataGrid>
C#
public MainPage()
{
// Required to initialize variables
InitializeComponent();
string data = "1,2,3,4,5,6,7,8,9";
Grid1.ItemsSource = data.Split(',');
}
When a button is clicked I want to be able to:
a) Find out which radio button was selected.
b) Get the data from one of the cells in the grid which corresponds to the selected radio button.
Is there an easy way to do this? There doesnt seem to be a rows collection on the grid. Or do I have to bind it to a datasource and then check the data source?
Many thanks.
The way I’d prefer to do this would be to bind
IsCheckedto a property of objects assigned to the ItemsSource. But here I’ll show you the hard way to do it(Edit: Actually the following is over complicated for this scenario but I’ll leave it here for now, see edits after)
First you need one of my VisualTreeEnumeration extension methods:-
Now in my testing I’ve just added a
ListBoxwith the namelstOutputto my Xaml. Now add the following couple of event handlers to youUserControl:-and finally tweak the Radio button Xaml like so:-
(One of the neat things about Xaml wiring up events is that it works even when the elements are part of a Template).
You’ll note that in the event handlers I’m walking up the visual tree from the sending
RadioButtonto find the containingDataGridRow. TheDataGridRowis the object that itsDataContextset to the object being rendered by that row. In your own code you could cast the data context value to the correct type and from there access other data about the row.Edit
Actually in most ordinary cases you don’t need to hunt down the owning
DataGridRowobject accessing theDataContextproperty of the sendingRadioButtonis sufficient:-Hence you can dispense with the
Ancestorsextension method. However in more complex cases where theDataContexthave been changed the original “over-complicated” approach may be needed.