如何使用ObservableCollection源实现XAML单选按钮控件?

Edw*_*uay 6 wpf xaml combobox radio-button

我在XAML中有以下ComboBox元素:

<ComboBox ItemsSource="{Binding CollectionControlValues}"
    SelectedItem="{Binding CollectionControlSelectedValue, UpdateSourceTrigger=PropertyChanged}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)

我想实现单选按钮同样的方式,就像这样:

伪代码:

<RadioButtons ItemsSource="{Binding CollectionControlValues}"
    SelectedItem="{Binding CollectionControlSelectedValue, UpdateSourceTrigger=PropertyChanged}">
    <RadioButtons .ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </RadioButtons .ItemTemplate>
</RadioButtons >
Run Code Online (Sandbox Code Playgroud)

但是,我能找到的唯一WPF RadioButton实现是静态的.

<StackPanel x:Name="rbHolder1" Style="{StaticResource rbStackPanelStyle}">
    <RadioButton Style="{StaticResource rbStyle}">RadioButton 1</RadioButton>
    <RadioButton Style="{StaticResource rbStyle}">RadioButton 2</RadioButton>
    <RadioButton Style="{StaticResource rbStyle}">RadioButton 3</RadioButton>
    <RadioButton Style="{StaticResource rbStyle}">...</RadioButton>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

如何创建一个像上面那样不是静态的RadioButton控件,而是从上面的ComboBox示例中的ItemsSource属性中获取数据?

gim*_*lay 5

使用ItemsControl和DataTemplate:

<ItemsControl ItemsSource="{Binding CollectionControlValues}">
  <DataTemplate>
    <RadioButton Content="{Binding Value} IsChecked={Binding SomeProperty}" GroupName="name"/>
  </DataTemplate>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)