0

First of all I'm new to WPF, I'm using MVVM Light Toolkit and for about 2 days now I'm scraping the internet trying to find a easy way to create a radio button list. I found many examples that were either too complex in my opinion, either "hacks" ish to some old bug or even non working examples.

Let's say that in your code-behind you have this list of strings

List<string> options = new List<string>();

        options.Add("option 1");
        options.Add("option 2");
        options.Add("option 3");
        options.Add("option 4");

So I want to ask you, what is the simplest way to create a radio button list with options?

2 Answers 2

1

I think, the easiest one is:

<ItemsControl ItemsSource="{Binding Options}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <RadioButton Content="{Binding}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

where Options is a property of your data context, like this:

public IEnumerable<string> Options
{
    get { return options; }
}

But I think, that you'll want to get selection result back.
So, the task becomes more complex. You need view model:

public class OptionViewModel
{
    public bool? IsChecked { get; set; }
    public string OptionName { get; set; }
}

Then, you must convert list of string to the list of view models:

public IEnumerable<OptionViewModel> Options
{
    get { return optionsAsViewModels ?? (optionsAsViewModels = new List(options.Select(_ => new OptionViewModel { OptionName = _ }))); }
}
private IEnumerable<OptionViewModel> optionsAsViewModels;

and make some changes into item template:

<ItemsControl ItemsSource="{Binding Options}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <RadioButton Content="{Binding OptionName}" IsChecked="{Binding IsChecked}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
Sign up to request clarification or add additional context in comments.

Comments

1

try below sample of code :-

 <DataTemplate>
               <RadioButton GroupName="Test"
                            Content="{Binding ItemDescription}"
                            IsChecked="{Binding IsSelected}"
                            Margin="5,1"/>
           </DataTemplate>

and in server side :-

public ViewModel()
        {
            Test = new Collection<SelectableItem>
                {
                    new SelectableItem { ItemDescription = "Option 1"},
                    new SelectableItem { ItemDescription = "Option 2"},
                    new SelectableItem { ItemDescription = "Option 3", IsSelected = true},
                    new SelectableItem { ItemDescription = "Option 4"}
                };
        }

and

public class SelectableItem : INotifyPropertyChanged
    {
        public string ItemDescription { get; set; }

        public bool IsSelected { get; set; }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.