简体   繁体   English

如何检查组中的第一个单选按钮

[英]How to check first radiobutton in group

I dynamically create a collection of radiobuton s.我动态创建了一个radiobuton的集合。

<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?默认情况下,如何将其中的第一个设置为IsChecked=true Ideally will be to do it in xaml .理想情况下将在xaml

You should set the corresponding IsSelected source property to true in your view model.您应该在视图模型中将相应的IsSelected源属性设置为true

View:查看:

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

View Model:查看型号:

YourItemsSourceCollection[0].IsSelected = true;

That's how MVVM and data binding works.这就是 MVVM 和数据绑定的工作原理。 You set the initial value of the property that the CheckBox in the view binds to.您设置视图中CheckBox绑定到的属性的初始值。 The XAML markup shouldn't contain any logic that decides which value is selected initially. XAML 标记不应包含任何决定最初选择哪个值的逻辑。 This logic belongs to the view model.这个逻辑属于视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM