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

发布于 2024-08-04 23:15:58 字数 1596 浏览 1 评论 0原文

我在 XAML 中有以下 ComboBox 元素:

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

我想以相同的方式实现 RadioButtons,如下所示:

PSEUDO-CODE :

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2024-08-11 23:15:58

使用 ItemsControl 和 DataTemplate:

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

Use ItemsControl and DataTemplate:

<ItemsControl ItemsSource="{Binding CollectionControlValues}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <RadioButton Content="{Binding Value}" IsChecked="{Binding SomeProperty}" GroupName="name"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
奢华的一滴泪 2024-08-11 23:15:58

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();

    }

}

}

我认为这会帮助您..

code in window1.xaml file

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

this line

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

is using itemsource property

C# code

namespace RadioButton
{
   public enum RadioOption
   {
    option1,
    option2,
    option3,
    option4
   }

public partial class Window1 : Window

{
    public Window1()

    {

        InitializeComponent();

    }

}

}

i think this will help you out..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文