Silverlight get selected row data in DataGrid -


In my Silverlight application, I have defined a datagrid with a template column that has a radio button:

XML:

  & lt; Data: DataGrid x: name = "grid1" margin = "8" & gt; & Lt; Data: DataGrid.Columns & gt; & Lt; Data: DataGrade Template column header = "radiobutton" & gt; & Lt; Data: DataGridTemplateColumn.CellTemplate & gt; & Lt; DataTemplate & gt; & Lt; Radio button x: name = "rdbIndataGrid" IsChecked = "false" GroupName = "myGroup" /> & Lt; / DataTemplate & gt; & Lt; / Data: DataGridTemplateColumn.CellTemplate & gt; & Lt; / Data: DataGridTemplateColumn & gt; & Lt; / Data: DataGrid.Columns & gt; & Lt; / Data: Data grid & gt;  

C #

  public keypage () {// initializeComponent () required to start; 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 Was there.

b) Receive data from one of the grids that matches the selected radio button.

Is there an easy way to do this? There is no line item on the grid. Or do I have to bind it to the data source and then check the data source?

Many thanks.

The way I like to do this, that item code will be replaced with the property property IsChecked must be binded. But here I show you the hard way to do this

( edit : In fact for this scenario the following is complicated, but I will leave it for now, see < / P>

First you need one of your VisualTreeEnumeration extension methods: -

  Public static class VisualTreeEnumeration {Public static IEnumerable & lt; DependencyObject & gt; Ancestors (this dependency object root) {Dependent object current = visualtreehel R. GatePant (Route); while (current! = Null) {yield returns current; current = visualtriheler.gatepoint (current);}}}  

Now in my trial I have only one < You can add the following codes to UserControl : -

  Private Zero rdbIndataGrid_Checked (Object Sender, RoutedEventArgs E) {DataGridRow line = ((dependent object) sender) .Ancestors () OfType & LT; DataGridRow & gt; () FirstOrDefault () .; If (line! = Null) lstOutput.Items.Add (string.format ("checked: {0}", line.datcontext)); } Private Zero rdbIndataGrid_Unchecked (Object Sender, RoutedEventArgs E) {DataGridRow line = ((dependent object) sender). Ancestors () OfType & lt; DataGridRow & gt; (). FirstOrDefault (); If (line! = Null) lstOutput.Items.Add (string.format ("uncheck: {0}", line.datcontext)); }  

and finally refresh the radio button Xaml like this: -

  & lt; Radio button x: name = "rdbIndataGrid" IsChecked = "false" GroupName = "MyGroup" checked = "rdbIndataGrid_Checked" uncheck = "rdbIndataGrid_Unchecked" />  

(One of the clearer things about Xaml wiring events is that even when the element is part of a blueprint, it still works).

Note that I'm running the View tree to find DataGridRow to find the scene handler I see > . DataGridRow is the object that is set to the object provided by that code in the datacentex in your own code, you can insert the data reference value in the correct type and the row Can access other data.

Edit

In general, you can send DataGridRow to DataContext in general cases : -

  Private zero rdbIndataGrid_Checked (Object Sender, RoutedEventArgs e) {Object myData = (FrameworkElement) Sender) .DataContext; If (myData! = Null) lstOutput.Items.Add (string.format ("checked: {0}", myData)); } Private Zero rdbIndataGrid_Unchecked (Object Sender, RoutedEventArgs e) {Object myData = (FrameworkElement) Sender) .DataContext; If (myData! = Null) lstOutput.Items.Add (string.format ("uncheck: {0}", mardeta)); }  

Therefore you can distribute with the ancestors expansion method. Although in more complex cases where DataContext has been changed, the original "highly complex" approach may be required.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -