【问题标题】:How to implement a XAML Radio Button control with an ObservableCollection source?如何使用 ObservableCollection 源实现 XAML 单选按钮控件?
【发布时间】:2010-11-28 09:34:05
【问题描述】:

我在 XAML 中有以下 ComboBox 元素:

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

我想以同样的方式实现RadioButtons,像这样:

伪代码:

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

但是,我能找到的唯一 WPF RadioButton 实现是 static 像这样。

<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 控件,它不像上面那样是静态的,而是从上面的 ComboBox 示例中的 ItemsSource 属性中获取其数据?

【问题讨论】:

    标签: wpf xaml combobox radio-button


    【解决方案1】:

    使用 ItemsControl 和 DataTemplate:

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

    【讨论】:

    • 您不需要将 包装在 中吗?至少 DataContext 仍然引用父级的 DataContext 而不是项目类型。见:stackoverflow.com/q/1511516/134761
    • “您不需要将 包装在 中吗?”是的。
    【解决方案2】:

    window1.xaml 文件中的代码

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

    这一行

    <ListBox` Margin="37,20,28,58" Name="listBox1" Style="{StaticResource RadioButtonList}" 
     ItemsSource="{Binding Source={StaticResource RadioOptions}}" 
    

    正在使用 itemsource 属性

    C#代码

    namespace RadioButton
    {
       public enum RadioOption
       {
        option1,
        option2,
        option3,
        option4
       }
    
    public partial class Window1 : Window
    
    {
        public Window1()
    
        {
    
            InitializeComponent();
    
        }
    
    }
    
    }
    

    我想这会对你有所帮助..

    【讨论】:

    • 您可能应该使用ItemsControl 而不是ListBox,因为后者的选择功能不是必需的。
    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    • 2017-04-27
    • 2017-06-11
    • 1970-01-01
    • 2020-11-19
    • 2011-08-28
    • 2013-05-29
    相关资源
    最近更新 更多