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

6
我在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 实现都是像这样的 静态 实现。
<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>

如何创建一个单选按钮控件,它不像上面的静态控件那样,而是从其ItemsSource属性中获取数据,就像上面ComboBox的例子一样?
2个回答

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

1
你不需要将<DataTemplate>包装在<ItemsControl.ItemTemplate>中吗?至少DataContext仍然是指向父级的DataContext而不是项类型。参见:https://dev59.com/73I_5IYBdhLWcg3wJPZu - angularsen
“你不需要将<DataTemplate>包装在<ItemsControl.ItemTemplate>中吗?” “是的。” - Sören Kuklau

1

在 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,因为后者的选择功能并不需要。 - Dan Puzey

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接