EgoPingvina
EgoPingvina

Reputation: 823

How to check first radiobutton in group

I dynamically create a collection of radiobutons.

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <RadioButton GroupName="CurrentParameter"
                     IsChecked="{Binding IsSelected, Mode=TwoWay}"
                     Content="{Binding Name, Mode=OneWay}"/>
    </DataTemplate>
</ItemsControl.ItemTemplate>

How can I set first of them to IsChecked=true by default? Ideally will be to do it in xaml.

Upvotes: 0

Views: 1028

Answers (1)

mm8
mm8

Reputation: 169150

You should set the corresponding IsSelected source property to true in your view model.

View:

<ItemsControl ItemsSource="{Binding YourItemsSourceCollection}" ... />

View Model:

YourItemsSourceCollection[0].IsSelected = true;

That's how MVVM and data binding works. You set the initial value of the property that the CheckBox in the view binds to. The XAML markup shouldn't contain any logic that decides which value is selected initially. This logic belongs to the view model.

Upvotes: 3

Related Questions