Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 580453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:28:29+00:00 2026-05-13T14:28:29+00:00

In my Silverlight application I have defined a datagrid with an template column containing

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:28:29+00:00Added an answer on May 13, 2026 at 2:28 pm

    The way I’d prefer to do this would be to bind IsChecked to 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:-

    public static class VisualTreeEnumeration
    {
    
        public static IEnumerable<DependencyObject> Ancestors(this DependencyObject root)
        {
            DependencyObject current = VisualTreeHelper.GetParent(root);
            while (current != null)
            {
                yield return current;
                current = VisualTreeHelper.GetParent(current);
            }
        }
    }
    

    Now in my testing I’ve just added a ListBox with the name lstOutput to my Xaml. Now add the following couple of event handlers to you UserControl :-

        private void rdbIndataGrid_Checked(object sender, RoutedEventArgs e)
        {
            DataGridRow row = ((DependencyObject)sender).Ancestors().OfType<DataGridRow>().FirstOrDefault();
            if (row != null)
                lstOutput.Items.Add(String.Format("Checked: {0}", row.DataContext));
        }
    
        private void rdbIndataGrid_Unchecked(object sender, RoutedEventArgs e)
        {
            DataGridRow row = ((DependencyObject)sender).Ancestors().OfType<DataGridRow>().FirstOrDefault();
            if (row != null)
                lstOutput.Items.Add(String.Format("Unchecked: {0}", row.DataContext));
        }
    

    and finally tweak the Radio button Xaml like so:-

    <RadioButton x:Name="rdbIndataGrid" IsChecked="false" GroupName="myGroup"
        Checked="rdbIndataGrid_Checked" Unchecked="rdbIndataGrid_Unchecked"  />
    

    (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 RadioButton to find the containing DataGridRow. The DataGridRow is the object that its DataContext set 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 DataGridRow object accessing the DataContext property of the sending RadioButton is sufficient:-

        private void rdbIndataGrid_Checked(object sender, RoutedEventArgs e)
        {
            object myData = ((FrameworkElement)sender).DataContext;
    
            if (myData != null)
                lstOutput.Items.Add(String.Format("Checked: {0}", myData));
        }
    
        private void rdbIndataGrid_Unchecked(object sender, RoutedEventArgs e)
        {
            object myData = ((FrameworkElement)sender).DataContext;
            if (myData != null)
                lstOutput.Items.Add(String.Format("Unchecked: {0}", myData));
        }
    

    Hence you can dispense with the Ancestors extension method. However in more complex cases where the DataContext have been changed the original “over-complicated” approach may be needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.