I have a gridview in wpf and have a two radio buttons and a button in template column. How do i access the status of radio buttons in the click event of the button?
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding TrackingID}" Header="TrackingID" Visibility="Hidden" Width="50" />
<DataGridTextColumn Binding="{Binding UserFullName}" Header="Name" Width="140" />
<DataGridTemplateColumn Width="350">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<RadioButton Width="50" Content="Yes" GroupName="status" />
<RadioButton Width="50" Content="No" GroupName="status" IsChecked="True" />
<Button Width="100" Click="Button_Click">View Details</Button>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
I am capturing the event for the button in Button_Click() function. In this function i want to know which radio button has been selected.
private void Button_Click(object sender, RoutedEventArgs e)
{
int trackingid = Convert.ToInt32((((FrameworkElement)sender).DataContext as DataRowView)[0].ToString()); //get the tracking id of the request
//(((FrameworkElement)sender).DataContext as RadioButton) //not working
}
Kindly help.
If you don’t want to use binding (in case when your Yes/No is purely a UI property or by whatever reason, you still have an option. Give your RadioButtons names by using x:Name – smth. like “YesRadioButton” and “NoRadiOButton” and then:
Call
FinName("YesRadioButton")for the shared container (parent of yourRadioButtons).or
Use
ElementNamebinding, i.e. bind your button’s tag to IsChecked of any of yourRadiButtonsthen in your button click simply inspect sender’s
Tagproperty.