首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ObservableCollection源代码实现XAML无线按钮控件?

如何使用ObservableCollection源代码实现XAML无线按钮控件?
EN

Stack Overflow用户
提问于 2009-09-15 08:47:52
回答 2查看 7.5K关注 0票数 6

XAML中有以下ComboBox元素:

代码语言:javascript
复制
<ComboBox ItemsSource="{Binding CollectionControlValues}"
    SelectedItem="{Binding CollectionControlSelectedValue, UpdateSourceTrigger=PropertyChanged}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

我希望在RadioButtons中以的方式实现,如下所示:

伪码:

代码语言:javascript
复制
<RadioButtons ItemsSource="{Binding CollectionControlValues}"
    SelectedItem="{Binding CollectionControlSelectedValue, UpdateSourceTrigger=PropertyChanged}">
    <RadioButtons .ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </RadioButtons .ItemTemplate>
</RadioButtons >

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

代码语言:javascript
复制
<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>

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

EN

回答 2

Stack Overflow用户

发布于 2009-09-15 09:24:29

使用ItemsControl和DataTemplate:

代码语言:javascript
复制
<ItemsControl ItemsSource="{Binding CollectionControlValues}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <RadioButton Content="{Binding Value}" IsChecked="{Binding SomeProperty}" GroupName="name"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
票数 5
EN

Stack Overflow用户

发布于 2009-09-15 09:39:33

window1.xaml文件中的代码

代码语言:javascript
复制
<Window x="RadioButton.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local ="clr-namespace:RadioButton"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<StackPanel>
    <StackPanel.Resources>
        <ObjectDataProvider x:Key="RadioOptions" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:RadioOption" />
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
        <Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}">

            <Setter Property="BorderBrush" Value="{x:Null}" />

            <Setter Property="BorderThickness" Value="0" />

            <Setter Property="ItemContainerStyle">

                <Setter.Value>

                    <Style TargetType="{x:Type ListBoxItem}" >

                        <Setter Property="Margin" Value="2" />

                        <Setter Property="Template">

                            <Setter.Value>

                                <ControlTemplate TargetType="{x:Type ListBoxItem}">

                                    <Border Background="Transparent">

                                        <RadioButton Focusable="False"

                    IsHitTestVisible="False"

                    IsChecked="{TemplateBinding IsSelected}">

                                            <ContentPresenter />

                                        </RadioButton>

                                    </Border>

                                </ControlTemplate>

                            </Setter.Value>

                        </Setter>

                    </Style>

                </Setter.Value>

            </Setter>

        </Style>
    </StackPanel.Resources>
    <ListBox Margin="37,20,28,58" Name="listBox1" Style="{StaticResource RadioButtonList}" ItemsSource="{Binding Source={StaticResource RadioOptions}}"  />
</StackPanel>

这条线

代码语言:javascript
复制
<ListBox` Margin="37,20,28,58" Name="listBox1" Style="{StaticResource RadioButtonList}" 
 ItemsSource="{Binding Source={StaticResource RadioOptions}}" 

正在使用itemsource属性。

C#代码

代码语言:javascript
复制
namespace RadioButton
{
   public enum RadioOption
   {
    option1,
    option2,
    option3,
    option4
   }

public partial class Window1 : Window

{
    public Window1()

    {

        InitializeComponent();

    }

}

}

我想这会帮到你..。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1425968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档