0

I Have a bound Data grid view to the below Questions Class:

public class Questions()

{

public int QuestionId{get; set;}
public string Question {get; set;}
public List<Answers> AvailableAnswers {get; Set;}
public string SelectedAnswer {get; set;}

}

public class Answers()
{

public int AnswerId {get; set;}
public string Answer {get; set;}
public bool IsSelected {get; set;}

}

What I need is within my Datagrid to show the Available Answers as Radio buttons and when the user selects one of the radio buttons for the AnswerId to be set as the SelectedAnswer property in the Questions Class.

Can anyone help as i have been going round in circles trying to do this

MichaelS
  • 7,023
  • 10
  • 51
  • 75
Bruie
  • 1,195
  • 3
  • 11
  • 18
  • Maybe you need a custom IValueConverter (SL standard answer #2). – jv42 Dec 13 '11 at 17:05
  • Check out this answer to the same question: http://stackoverflow.com/questions/2284752/mvvm-binding-radio-buttons-to-a-view-model – opedog Dec 13 '11 at 17:26

1 Answers1

0

There are a few ways you can do this if you are using MVVM within your view-model you can create a public property such as

private bool _isAnswer1;
    public bool IsAnswer1
    {
        get { return _isAnswer1; }
        set
        {
            _isAnswer1 = value;
            NotifyPropertyChanged(m => m.IsAnswer1); //I used i notify property changed but this is inherited from the base class
        }
    }

And then in the UI binding similar to

<CheckBox x:Name="testCheckBox" IsChecked="{Binding IsAnswer1}   />

Assuming you have the data context set at the grid or main view to the view model.

You could then bind this property to the UI and when checked it could then invoke a different action or method for another element. It just depends upon how you implement this.

If you are not using mvvm and you want to handle this in the ui you can use elementName binding. Here you basically bind property of one element on the value of another (Example check a checkbox and have a value appear in the UI) Here is a link from MSDN on element name binding MSDN Link Here

rlcrews
  • 3,482
  • 19
  • 66
  • 116