0

I have a list of objects (consisting of an ID of type int and a String), each represent an option and corresponds to a radio box. I have an int selectedChoice, and it stands for the ID of the selected radiobox, which represent an option. I want the int SelectedChoice and the actual selected radio box binded to each other (two-way binding).

The radioboxes are added into the listbox using ItemTemplate and ItemsSource.

How can this be done? I've tried so many methods and no one works.


I have tried

  • Converter (but it doesn't support binding in ConverterParameter)
  • Add a whole class which can identify the chosen radio box, and also carries the int SelectedChoice. But this method fails when converting back, as I don't get any necessary information that way.
  • Use DependencyObject, which fails when I'm trying to bind things to it, as there isn't DataContext in Binding.Converter and workarounds I found on the Internet works for WPF only.
  • IMultiConverter which doesn't exist at all in SilverLight
Haozhun
  • 6,331
  • 3
  • 29
  • 50

2 Answers2

1
public class Choice : BaseModel
{
    private int id;
    public int Id
    {
        get { return id; }
        set 
        {
            id = value;
            NotifyPropertyChanged("Id");
        }
    }

    private bool isSelected;
    public bool IsSelected
    {
        get { return isSelected; }
        set
        {
            isSelected = value;

            if (isSelected)
            {
                App.viewModel.SelectItem(Id);
            }
            NotifyPropertyChanged("IsSelected");
        }
    }

    private string text;
    public string Text
    {
        get { return text; }
        set
        {
            text = value;
            NotifyPropertyChanged("Text");
        }
    }
}

.

public class ViewModel : BaseViewModel
{
    public ObservableCollection<Choice> Choices {get; set;}

    public int SelectedChoice { get; private set; }

    public ViewModel()
    {
        Choices = new ObservableCollection<Choice>();
        Choices.Add(new Choice() { Id = 0, Text = "1" });
        Choices.Add(new Choice() { Id = 1, Text = "2" });
        Choices.Add(new Choice() { Id = 2, Text = "3" });
    }

    public void SelectItem(int Id)
    {
        SelectedChoice = Id;
        MessageBox.Show(SelectedChoice.ToString());
        foreach (var choice in Choices)
        {
            if (choice.Id != Id)
            {
                choice.IsSelected = false;
            }
        }
    }
}
Ku6opr
  • 8,126
  • 2
  • 24
  • 31
1

You could style a listbox to have radiobuttons a listboxitems. The listbox itemssource would be bound to the list of your items. Then use the selecteditem or selectedvalue property as normal.

How to implement a XAML Radio Button control with an ObservableCollection source?

Binding selectedItem ListBox of Radio buttons in combobox popup

Community
  • 1
  • 1
Josh C.
  • 4,303
  • 5
  • 30
  • 51
  • This was exactly what I was thinking about: I was doing the whole thing wrong. I just wasn't on the right path to the solution. Thank you! – Haozhun Dec 09 '11 at 03:11