本文介绍了如何使用ObservableCollection源实现XAML Radio Button控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XAML中有以下 ComboBox 元素:

 < ComboBox ItemsSource ={ Binding CollectionControlValues}
SelectedItem ={Binding CollectionControlSelectedValue,UpdateSourceTrigger = PropertyChanged}>
< ComboBox.ItemTemplate>
< DataTemplate>
< TextBlock Text ={绑定值}/>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>

我想以同样的方式实现 RadioButtons / strong>,例如:



> <
><
> < RadioButtons ItemsSource ={Binding CollectionControlValues}
SelectedItem ={Binding CollectionControlSelectedValue,UpdateSourceTrigger = PropertyChanged}>
< RadioButtons .ItemTemplate>
< DataTemplate>
< TextBlock Text ={绑定值}/>
< / DataTemplate>
< / RadioButtons .ItemTemplate>
< / RadioButtons>然而,我可以找到的唯一WPF RadioButton实现是静态,像这样。

 < StackPanel x:Name =rbHolder1Style ={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>

如何创建一个RadioButton控件不是静态的,

解决方案

使用ItemsControl和DataTemplate:

 < ItemsControl ItemsSource ={Binding CollectionControlValues}> 
< DataTemplate>
< RadioButton Content ={Binding Value} IsChecked = {Binding SomeProperty}GroupName =name/>
< / DataTemplate>
< / ItemsControl>


I have the following ComboBox element in XAML:

<ComboBox ItemsSource="{Binding CollectionControlValues}"
    SelectedItem="{Binding CollectionControlSelectedValue, UpdateSourceTrigger=PropertyChanged}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

I would like to implement RadioButtons in the same way, like this:

PSEUDO-CODE:

<RadioButtons ItemsSource="{Binding CollectionControlValues}"
    SelectedItem="{Binding CollectionControlSelectedValue, UpdateSourceTrigger=PropertyChanged}">
    <RadioButtons .ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </RadioButtons .ItemTemplate>
</RadioButtons >

However, the only WPF RadioButton implementations I can find are static like this.

<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>

How can I create a RadioButton control which is not static like the above but instead gets its data from its ItemsSource property as in the above ComboBox example?

解决方案

Use ItemsControl and DataTemplate:

<ItemsControl ItemsSource="{Binding CollectionControlValues}">
  <DataTemplate>
    <RadioButton Content="{Binding Value} IsChecked={Binding SomeProperty}" GroupName="name"/>
  </DataTemplate>
</ItemsControl>

这篇关于如何使用ObservableCollection源实现XAML Radio Button控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 15:25